Search code examples
iphoneuikit

UIButtons at the bottom of a UIScrollView are not receiving touches


I am writing an iPhone app with a tab bar and navigation bar. At a certain point, I am pushing an instance of my DetailsViewController class onto the navigation controller stack to show it.

This controller creates its view hierarchy in code: the controller's view property is set to a UIScrollView, which contains a plain UIView (let's call it "contentView") sized to hold all the content to be shown. At the bottom of this contentView, I have four UIButtons.

Now when I run the app (in the simulator at present), and scroll to the bottom of the view, the top two buttons respond to touches; the third responds to touches only in the top portion of it, and the lower button doesn't respond to touches at all. By clicking in various parts of the third button, it appears that the lower 93 pixels of the scroll view is not passing touch events through to its subviews.

93 is suspicious: it's also the combined height of the tab bar (49 pixels) and navigation bar (44 pixels). Yet the navigation bar and tab bar are outside the scroll view. Any suggestions why this might be happening?

Here's the code in question:

- (void)loadView
{
    CGRect frame = [[UIScreen mainScreen] applicationFrame]; 
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
    scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    scrollView.delegate = self;
    self.view = scrollView;

    UIView *contentView = [[UIView alloc] initWithFrame:scrollView.bounds];
    contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    [scrollView addSubview:contentView];

    CGSize viewSize = contentView.bounds.size;
    CGSize size;
    CGFloat y = 0;

    /* Snip creating various labels and image views */

    /* Actions view creates and lays out the four buttons; its sizeThatFits:
    ** method returns the frame size to contain the buttons */

    actionsView = [[PropertyDetailActionsView alloc] initWithFrame:CGRectZero];
    actionsView.autoresizingMask = (UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth);
    actionsView.delegate = self;
    size = [actionsView sizeThatFits:viewSize];
    actionsView.frame = CGRectMake(0, y, size.width, size.height);
    [contentView addSubview:actionsView];
    y += size.height;

    [contentView setFrame:CGRectMake(0, 0, viewSize.width, y)];
    scrollView.contentSize = contentView.frame.size;

    [contentView release];
    [scrollView release];
}

Solution

  • As I suggested on Twitter yesterday, it may have something to do with the flexible bottom margin set to the actionsView.

    That suggestion did not resolve the problem, yet it lead to the right direction. By removing the flexible height of the contentView the problem has been fixed.

    So if anyone out there is having similar problems, try to play with your autoresizingMasks.