【问题标题】:UIButton not Responsive When placed out of its container UIScrollViewUIButton 不响应当放置在其容器 UIScrollView 之外时
【发布时间】:2017-01-12 07:39:26
【问题描述】:

我有一个 UIScrollView。在这个 UIScrollView 内部是一个 UIView,它包含所有其他子视图(包括一个 UIButton)。它与 UIScrollView 具有相同的边界。

我想要:

  1. UIScrollView 的底部不可滚动(放置 UIButton 的位置)。
  2. UIButton 是响应式的。

所以我将 UIScrollView 的底部设置为比 UIVIew 高一点,如下图所示:

这将使 UIButton 不响应(它在左侧布局中响应)。有什么建议吗?谢谢!

我的部分代码:

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[self addSubview:self.scrollView];
self.scrollContent = [[UIView alloc] initWithFrame:CGRectZero];
[self.scrollView addSubview:self.scrollContent];

self.button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.scrollContent addSubview:self.button];
[self.button addTarget:self
                action:@selector(buttonPressed)
      forControlEvents:UIControlEventTouchUpInside];

更新:

看起来正确的做法是保持滚动视图的底部并设置它的contentSize,但是由于我使用的是autolayout,我发现contentSize的高度总是为零???

【问题讨论】:

  • 好像不行。另一个问题是我最初在滚动视图之外放置了一个按钮。滚动后,按钮也没有响应...

标签: ios objective-c uiscrollview uibutton


【解决方案1】:

您需要将按钮放置在滚动视图视图层次结构之外的视图中。

当前命中测试失败,因为按钮在滚动视图边界之外但在其层次结构内。

【讨论】:

    【解决方案2】:

    然后尝试以下代码将按钮添加到滚动视图中:

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        [self addSubview:self.scrollView];
        self.scrollContent = [[UIView alloc] initWithFrame:CGRectZero];
        [self.scrollView addSubview:self.scrollContent];
    
            self.scrollView.delaysContentTouches = NO; //Add this line
    
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.scrollContent addSubview:self.button];
        [self.button addTarget:self
                        action:@selector(buttonPressed)
              forControlEvents:UIControlEventTouchUpInside];
    

    【讨论】:

      猜你喜欢
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 2011-08-31
      • 2014-01-03
      • 2016-12-15
      • 1970-01-01
      相关资源
      最近更新 更多