Mark Kennedy
Mark Kennedy

Reputation: 1781

UIButton inside UIScrollView disables scrolling

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

Upvotes: 2

Views: 2438

Answers (3)

Userich
Userich

Reputation: 316

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

Upvotes: 0

Mark Kennedy
Mark Kennedy

Reputation: 1781

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

Upvotes: 2

Abdullah Shafique
Abdullah Shafique

Reputation: 6918

In viewDidLoad add:

self.scroller.canCancelContentTouches = YES;

Upvotes: 0

Related Questions