【问题标题】:UIButton inside UIScrollView disables scrollingUIScrollView 内的 UIButton 禁用滚动
【发布时间】:2013-08-05 12:02:49
【问题描述】:

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

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

//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)];
}

【问题讨论】:

标签: ios xcode uiscrollview uibutton


【解决方案1】:

其实就是在将代码移动到viewDidLayoutSubiews之后才起作用的。

有人能解释一下为什么会这样吗?

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

【讨论】:

    【解决方案2】:

    在 viewDidLoad 中添加:

    self.scroller.canCancelContentTouches = YES;
    

    【讨论】:

    • 对为什么这是必要的一些解释可能会有所帮助。
    【解决方案3】:

    继承 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中找到

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 2013-03-26
      • 2014-10-10
      相关资源
      最近更新 更多