【问题标题】:UIScrollView subview not always clickableUIScrollView 子视图并不总是可点击的
【发布时间】:2013-03-13 19:21:51
【问题描述】:

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

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

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

这是我的代码:

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

这是很好理解我的问题的图像:

【问题讨论】:

    标签: objective-c uiscrollview uibutton touch addsubview


    【解决方案1】:

    您可以使用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];
    

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

    【讨论】:

    • 谢谢,我放了一些backgroundcolor 来了解我的视图和按钮以及按钮上方的一些视图。还是谢谢你
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      相关资源
      最近更新 更多