如何在UICollection补充视图中使用UIButton?

mar*_*son 9 cocoa-touch objective-c ios ios6 uicollectionview

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

在UICollection补充视图中实现UIButton需要做些什么吗?

rob*_*off 19

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

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

(lldb) po [[UIApp keyWindow] recursiveDescription]
Run Code Online (Sandbox Code Playgroud)

找到列表中的按钮并复制其地址.然后你可以检查它和每个超级视图.例:

(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>
Run Code Online (Sandbox Code Playgroud)

在这里,我发现所有的意见达根(中UIWindow)返回YESisUserInteractionEnabled.窗口的超级视图是零.