9

我正在尝试将 UIButton 放置在 UICollectionView 补充视图(页脚)中。我已经使用故事板将 UIButton 连接到 UICollectionViewCell 的子类,并且可以以编程方式更改它的属性,包括背景图像。但是,当我将按钮的 touch up inside 事件连接到一个方法时,它不会触发。事实上,按钮甚至看起来都没有对用户的触摸做出视觉响应。在故障排除中,我尝试将 UIButton 添加到集合视图的标题中并查看相同的行为。将按钮添加到不相关的视图会在按下时产生交互效果。

在 UICollection 补充视图中实现 UIButton 需要做一些特别的事情吗?

4

2 回答 2

19

补充视图应该是 a UICollectionReusableView(或其子类),而不是UICollectionViewCell.

无论如何,如果按钮没有响应,首先要做的是检查其所有祖先视图是否已userInteractionEnabled设置为YES. 一旦按钮出现在屏幕上,在调试器中暂停并执行以下操作:

(lldb) po [[UIApp keyWindow] recursiveDescription]

在列表中找到按钮并复制其地址。然后你可以检查它和每个superview。例子:

(lldb) p (BOOL)[0xa2e4800 isUserInteractionEnabled]
(BOOL) $4 = YES
(lldb) p (BOOL)[[0xa2e4800 superview] isUserInteractionEnabled]
(BOOL) $5 = YES
(lldb) p (BOOL)[[[0xa2e4800 superview] superview] isUserInteractionEnabled]
(BOOL) $6 = YES
(lldb) p (BOOL)[[[[0xa2e4800 superview] superview] superview] isUserInteractionEnabled]
(BOOL) $8 = YES
(lldb) p (BOOL)[[[[[0xa2e4800 superview] superview] superview] superview] isUserInteractionEnabled]
(BOOL) $9 = YES
(lldb) p (BOOL)[[[[[[0xa2e4800 superview] superview] superview] superview] superview] isUserInteractionEnabled]
(BOOL) $10 = NO
(lldb) po [[[[[0xa2e4800 superview] superview] superview] superview] superview]
(id) $11 = 0x00000000 <nil>

在这里,我发现直到根 (the UIWindow)的所有视图都YESisUserInteractionEnabled. 窗口的超级视图为零。

于 2012-11-26T20:19:02.440 回答
0

我有一个自定义按钮,它子类化UIControl并将其放在UICollectionReusableView. 为了使它工作,我使控件的每个子视图及其子视图的子视图不处理用户交互。

func disableUserInteractionInView(view: UIView) {
    view.userInteractionEnabled = false
    for view in view.subviews {
        self.disableUserInteractionInView(view)
    }
}

// My control has a subview called contentView which serves as a container
// of all my custom button's subviews.
self.disableUserInteractionInView(self.contentView)

Since adding that code, all event listeners to .TouchUpInside would fire, and the control will visually highlight when pressed down.

于 2016-08-25T12:33:16.410 回答