【问题标题】:UIButton not responding after set frame within UIScrollview在 UIScrollview 中设置框架后 UIButton 没有响应
【发布时间】:2011-08-31 21:17:24
【问题描述】:

我有一个 UIScrollView,它的内容是用 Interface Builder 设计的。它有一个表格,下面有一个 UIButton。如果按钮之前没有移动,它可以工作(touchesBegan 和 TouchUpInside 被调用),但如果它使用 'button.frame = ' 移动以响应内容增长(表格变大),它会停止响应任何触摸。

我验证了它前面没有隐藏视图,我什至使用了bringViewToFront。

【问题讨论】:

  • 请显示一些代码:您最初将按钮框架设置为什么,移动后将其设置为什么?
  • 感谢@Rayfleck,但@FelipeSabino 明白了。

标签: iphone objective-c cocoa-touch uibutton


【解决方案1】:

检查您的 UIButton 最终位置是否在 UITableViewUIScrollView 边界内。

有可能你移动之后UIBUtton被放到了边界之外,然后就不会响应触摸事件了。

一个可以让您验证的快速设置是将您的UITableViewUIScrollViewclipToBounds 属性设置为NO,然后放置在边界之外的所有内容甚至都不可见。

【讨论】:

  • 这正是问题所在。我正在调整scrollview.contentSize,但不是容器视图本身。谢谢。
  • 遇到了同样的问题。忘记将 contentSize.height 设置为有用的东西。这篇文章让我很容易找到问题所在。谢谢!
  • 感谢您的回答 - 我有一个非常相似的问题,在 UIScrollView 内的一个很长的视图内有一堆 UIButton 按钮。 “首屏下方”的按钮没有收到任何触摸事件。在长视图中将“剪辑子视图”设置为 true 揭示了问题 - 长视图被调整为短视图(1 个屏幕长度)。为了真正解决这个问题,我在滚动视图上将“Autoresize Subviews”设置为 false,现在它就像一个魅力。
  • 很好的解释,伙计。我增加了子视图的高度,但没有增加容器的高度。非常感谢。
【解决方案2】:

在我最近从事的一个项目中,我有一个 UITableView,在 FooterView 中有一个 UIButton。当您尝试滚动按钮时,由于它是此 UITableView 中的最后一项,因此该按钮将固定在视图的底部。

当我的 UITableView 的内容导致 contentSize 小于 UITableView 的高度时,我遇到了与这篇文章相同的问题。我的 UIButton 基本上超出了 UITableView 可滚动内容的初始帧的范围,因此没有收到任何事件。

我想发布我的解决方案,以便其他收到此行为的人可以得到帮助。

我必须在我的自定义 UITableView 类中覆盖 pointInside:withEventhitTest:withEvent 方法:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    BOOL pointInside = [super pointInside:point withEvent:event];

    if (!pointInside) {
        CGRect buttonFrame = [self convertRect:self.myButton.frame  fromView:self];
        if (CGRectContainsPoint(buttonFrame, point)) {
            return YES;
        }
    }

    return pointInside;
}


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {
        for (UIView *subview in self.subviews.reverseObjectEnumerator) {
            CGPoint subPoint = [subview convertPoint:point fromView:self];
            UIView *result = [subview hitTest:subPoint withEvent:event];

            if (result != nil) {
                return result;
            }
        }
    }

    // No other subviews have triggered this 'touch' check self.myButton
    CGPoint subPoint = [self.myButton convertPoint:point fromView:self];
    UIView *result = [self.myButton hitTest:subPoint withEvent:event];

    if (result != nil) {
        return result;
    }

    // Pass the 'touch' on if no subviews trigger the 'touch'
    return [super hitTest:point withEvent:event];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    相关资源
    最近更新 更多