【问题标题】:Moving a UIView containing UIButtons makes those buttons stop respond to taps移动包含 UIButtons 的 UIView 会使这些按钮停止响应点击
【发布时间】:2010-01-30 02:20:47
【问题描述】:

我在 .xib 中的 UIView 中的 UIScrollView 中有一个 UIView,其中包含多个按钮。如果我将 UIView 从 viewWillAppear 内向下移动几个像素,则按钮都会停止响应点击。

这是我用来调整 UIScrollView 大小并向下移动按钮的代码:

    // adjust the view height to accomodate the resized label
    scrollView.contentSize = CGSizeMake( 320, 367 + expectedLabelSize.height - originalLabelSize.height );

    // Adjust the location of buttons
    CGRect buttonsBounds = buttons.bounds;
    buttonsBounds.origin.y -= expectedLabelSize.height - originalLabelSize.height; //XX
    buttons.bounds = buttonsBounds;

如果我注释掉标记为 XX 的行,按钮可以正常工作,但当然是在错误的位置。

如果我尝试不同数量的像素(将expectedLabelSize.height - originalLabelSize.height 替换为硬编码值),我会得到有趣的结果。 10 像素工作正常。 50 像素使我的顶部按钮工作正常,但我的底部按钮失败。 100 像素,两个按钮都失败。 (-50) 像素导致底部按钮工作正常,但顶部按钮失败。

知道可能导致问题的原因吗?我是否需要以某种方式通知按钮其父视图已移动?

【问题讨论】:

    标签: iphone objective-c uiview


    【解决方案1】:

    问题是我应该使用buttons.frame 而不是buttons.bounds。改用框架解决了这个问题。

    【讨论】:

      【解决方案2】:

      我建议给父 UIView 一个背景颜色并将按钮样式设置为圆角。这样你就可以真正看到屏幕上正在发生的事情。 (假设您在某些图像上使用透明按钮)

      【讨论】:

      • 不错的建议,帮助我弄清楚发生了什么。干杯!
      猜你喜欢
      • 2013-01-10
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多