UIScrollView中的UIButton禁用滚动

时间:2013-08-04 19:59:35

标签: ios xcode uiscrollview uibutton

将按钮拖动到我的滚动视图后,窗口不再滚动!如果我删除按钮,滚动现在可以正常工作。

之前有人遇到过这个问题吗?

//ScrollViewController.h
@property (weak, nonatomic) IBOutlet UIScrollView *scroller;


//ScrollViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.scroller setScrollEnabled:YES];
    [self.scroller setContentSize:CGSizeMake(320, 700)];
}

enter image description here

3 个答案:

答案 0 :(得分:2)

实际上,在将代码添加到viewDidLayoutSubiews之后,它才起作用。

有人可以解释为什么这有用吗?

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self.scroller setScrollEnabled:YES];
    [self.scroller setContentSize:CGSizeMake(320, 700)];
}

答案 1 :(得分:0)

在viewDidLoad中添加:

self.scroller.canCancelContentTouches = YES;

答案 2 :(得分:0)

将UIScrollView子类化并将下面的代码添加到.m文件中,确实解决了iOS 8下的滚动冻结问题。

代码:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
    UITouch *touch = [touches anyObject];

    if(touch.phase == UITouchPhaseMoved)
    {
        return NO;
    }
    else
    {
        return [super touchesShouldBegin:touches withEvent:event inContentView:view];
    }
}

这个解决方案见于pasta12的回答https://stackoverflow.com/a/25900859/3433059