我试图将 UIButton 放置在 UICollectionView 补充视图(页脚)中。我已使用故事板将 UIButton 连接到 UICollectionViewCell 的子类,并且可以以编程方式更改其属性,包括背景图像。但是,当我将按钮的 touch up inside 事件连接到方法时,它不会触发。事实上,该按钮甚至看起来没有对用户的触摸做出视觉响应。在故障排除中,我尝试将 UIButton 添加到集合视图的标题中,并看到相同的行为。将按钮添加到不相关的视图会在按下时产生交互效果。
我需要做一些特殊的事情才能在 UICollection 补充视图中实现 UIButton 吗?
补充观点应该是UICollectionReusableView
(或其子类),不是UICollectionViewCell
.
无论如何,如果按钮没有响应,首先要做的是检查它的所有祖先视图是否有userInteractionEnabled
set to YES
。按钮出现在屏幕上后,在调试器中暂停并执行以下操作:
(lldb) po [[UIApp keyWindow] recursiveDescription]
在列表中找到该按钮并复制其地址。然后你可以检查它和每个超级视图。例子:
(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>
在这里我发现所有视图直到根(UIWindow
) 返回YES
from isUserInteractionEnabled
。窗口的超级视图为零。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)