IPhone: adding a button to scrollview makes the button inaccessible for interaction

For some reason, the button initialized in the addBook method of my viewController will not respond to touch. The selector that I assigned to it never starts, and the UIControlStateHighlighted image never appears when I click on the image.

Are there any catching touches before they get into UIButton, or is its interactivity somehow turned off by what I'm doing with it?

- (void)viewDidLoad {

    ...

    _scrollView.contentSize = CGSizeMake(currentPageSize.width, currentPageSize.height);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.scrollsToTop = NO;
    _scrollView.pagingEnabled = YES;

    ...
}

- (void)addBook {
    // Make a view to anchor the UIButton
    CGRect frame = CGRectMake(0, 0, currentPageSize.width, currentPageSize.height);
    UIImageView* bookView = [[UIImageView alloc] initWithFrame:frame];

    // Make the button
    frame = CGRectMake(100, 50, 184, 157);
    UIButton* button = [[UIButton alloc] initWithFrame:frame];
    UIImage* bookImage = [UIImage imageNamed:kBookImage0];

    // THIS SECTION NOT WORKING!
    [button setBackgroundImage:bookImage forState:UIControlStateNormal];
    UIImage* bookHighlight = [UIImage imageNamed:kBookImage1];
    [button setBackgroundImage:bookHighlight forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(removeBook) forControlEvents:UIControlEventTouchUpInside];

    [bookView addSubview:button];   
    [button release];
    [bookView autorelease];

    // Add the new view/button combo to the scrollview.
    // THIS WORKS VISUALLY, BUT THE BUTTON IS UNRESPONSIVE :(
    [_scrollView addSubview:bookView];
}

- (void)removeBook {
    NSLog(@"in removeBook");
}

The view hierarchy looks like this in Interface Builder:

UIWindow
UINavigationController
    RootViewController
        UIView
            UIScrollView
            UIPageControl

and presumably how this happens when using the addBook method:

UIWindow
UINavigationController
    RootViewController
        UIView
            UIScrollView
                UIView
                    UIButton
            UIPageControl
+3
source share
2

+7

[bookView autorelease]; :

// Add the new view/button combo to the scrollview.
// THIS WORKS VISUALLY, BUT THE BUTTON IS UNRESPONSIVE :(
[_scrollView addSubview:bookView];
[bookView release];

_scrollView.canCancelContentTouches = YES;

delaymentsContentTouches - , , touch-down. YES, , , . NO, touchsShouldBegin: withEvent: inContentView:. : YES.

canCancelContentTouches - , , . YES, , , , Cancelled: withEvent: . NO, .

+4

Source: https://habr.com/ru/post/1734846/


All Articles