0
votes

I have a UIScrollView. Inside this UIScrollView is a UIView which contains every other child views (including a UIButton). It has the same bounds as the UIScrollView.

I want:

  1. The bottom of the UIScrollView is not scroll-able (where the UIButton is placed).
  2. The UIButton is responsive.

So I set the bottom of the UIScrollView a little higher than the UIVIew as below image:

enter image description here

This will make the UIButton not responsive (it's responsive in the left layout). Any suggestion? Thanks!

Some part of my code:

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[self addSubview:self.scrollView];
self.scrollContent = [[UIView alloc] initWithFrame:CGRectZero];
[self.scrollView addSubview:self.scrollContent];

self.button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.scrollContent addSubview:self.button];
[self.button addTarget:self
                action:@selector(buttonPressed)
      forControlEvents:UIControlEventTouchUpInside];

Update:

Looks like the right way is to keep the bottom of the scroll view and set its contentSize, but since I'm using autolayout, I found that the height of the contentSize is always zero???

2
Looks like it doesn't work. Another issue is i have a button placed outside of the scroll view initially. After I scroll it in, the button is also not responsive... - mlin956

2 Answers

0
votes

You need to place the button in a view that is outside of the scroll views view hierarchy.

Currently the hit testing is failing because the button is outside of the scroll views bounds but within its hierarchy.

0
votes

Add the button into scroll view after that try the below code:

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    [self addSubview:self.scrollView];
    self.scrollContent = [[UIView alloc] initWithFrame:CGRectZero];
    [self.scrollView addSubview:self.scrollContent];

        self.scrollView.delaysContentTouches = NO; //Add this line

    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.scrollContent addSubview:self.button];
    [self.button addTarget:self
                    action:@selector(buttonPressed)
          forControlEvents:UIControlEventTouchUpInside];