2

我正在将子视图添加到UIScrollView. 对于这个问题,我正在简化添加的视图:testView它包含一个UIButton

我的滚动视图运行良好,但按钮的触摸效果不佳

  • 我可以点击第一个按钮,但只能点击(近似的)100 first pixel
  • 滚动效果很好。
  • 我无法单击第一个按钮的末尾
  • 我无法点击其他按钮

这是我的代码:

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

这是很好理解我的问题的图像: 带有按钮不可触摸的uiscrollview

4

2 回答 2

0

您可以使用bringSubviewToFront来避免该问题

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

如果您再次遇到问题,请在按钮周围应用边框并检查手动可见的按钮区域。

于 2013-03-13T19:43:04.413 回答
0

我弄错了按钮和视图的框架。如果它可以帮助任何人,这里是一个工作代码。

__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)];
于 2013-03-14T17:58:28.727 回答