Question

I'm adding subviews to an UIScrollView. For this question I'm simplifying the added view : testView it contains an UIButton

My Scroll View is working great, but the touch on the buttons is not working well.

  • I can click on the first button but only on the (approximative) 100 first pixels.
  • the scrolling is working very well.
  • I cannot click on the end of the first button
  • I cannot click on the other buttons

here is my code :

__block CGFloat scrollViewContentSize = 0;
__block CGFloat buttonRectOrigineY = 0;

[self.itemsToDisplay enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    CGRect frameTest = CGRectMake(0, buttonRectOrigineY/2, 320, 200);
    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    testButton.frame = frameTest;
    UIView *testView = [[UIView alloc] initWithFrame:frameTest];
    [testView addSubview:testButton];
    [self.articleScrollView addSubview:testView];
    buttonRectOrigineY      += 200;
    scrollViewContentSize   += 200;
}];
[self.articleScrollView setContentSize:CGSizeMake(320, scrollViewContentSize)];

here is image to understand well my problem : uiscrollview with button not touchable

Was it helpful?

Solution 2

I was mistaking the frame of my button and view. Here a working code if it can help anyone.

__block CGFloat scrollViewContentSize = 0;
__block CGFloat buttonRectOrigineY = 0;

[self.itemsToDisplay enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    CGRect frameTestButton = CGRectMake(0, 0, 300, 150);
    CGRect frameTestView = CGRectMake(10, buttonRectOrigineY, 300, 200);
    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    testButton.frame = frameTestButton;
    testButton.backgroundColor = [UIColor greenColor];
    UIView *testView = [[UIView alloc] initWithFrame:frameTestView];
    if(idx == 0)
        testView.backgroundColor = [UIColor yellowColor];
    if(idx == 1)
        testView.backgroundColor = [UIColor orangeColor];
    if(idx == 2)
        testView.backgroundColor = [UIColor redColor];
    if(idx == 3)
        testView.backgroundColor = [UIColor magentaColor];
    if(idx == 4)
        testView.backgroundColor = [UIColor purpleColor];
    if(idx <= 4){
        LogDebug(@"idx : %lu", (unsigned long)idx);
        [testView addSubview:testButton];
        [self.articleScrollView addSubview:testView];
        [self.articleScrollView bringSubviewToFront:testView];
        buttonRectOrigineY      += 200;
        scrollViewContentSize   += 200;
    }
}];
[self.articleScrollView setContentSize:CGSizeMake(320, scrollViewContentSize)];

OTHER TIPS

You can use bringSubviewToFront to avoid the issue

CGRect frameTest = CGRectMake(0, buttonRectOrigineY/2, 320, 200);
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testButton.frame = frameTest;
UIView *testView = [[UIView alloc] initWithFrame:frameTest];
[testView addSubview:testButton];
[testView bringSubviewToFront:testButton];
[self.articleScrollView addSubview:testView];
[self.articleScrollView bringSubviewToFront:testView];

If you have issues again, apply boarder around the button and check it the button area that visible manually.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top