UIButton inside UIScrollView disables scrolling

2019-07-21 08:37发布

After dragging a button to my scrollview, the window no longer scrolls! If I remove the button, scrolling now works.

Anyone run into this issue before?

//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条回答
倾城 Initia
2楼-- · 2019-07-21 09:02

Actually, it just worked after adding moving the code to viewDidLayoutSubiews.

Can someone explain WHY this works?

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self.scroller setScrollEnabled:YES];
    [self.scroller setContentSize:CGSizeMake(320, 700)];
}
查看更多
我命由我不由天
3楼-- · 2019-07-21 09:06

In viewDidLoad add:

self.scroller.canCancelContentTouches = YES;
查看更多
太酷不给撩
4楼-- · 2019-07-21 09:14

Subclassing UIScrollView and adding code below into .m file, did solve my scrolling freeze problem under iOS 8.

Code:

- (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];
    }
}

This solution was found in pasta12's answer https://stackoverflow.com/a/25900859/3433059

查看更多
登录 后发表回答