如何在 UICollection 补充视图中使用 UIButton? [英] How can I use a UIButton in a UICollection supplementary view?

查看:14
本文介绍了如何在 UICollection 补充视图中使用 UIButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

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

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

(lldb) po [[UIApp keyWindow] recursiveDescription]

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

(lldb) p (BOOL)[0xa2e4800 isUserInteractionEnabled](布尔) $4 = 是(lldb) p (BOOL)[[0xa2e4800 superview] isUserInteractionEnabled](布尔) $5 = 是(lldb) p (BOOL)[[[0xa2e4800 superview] superview] isUserInteractionEnabled](布尔) $6 = 是(lldb) p (BOOL)[[[[0xa2e4800 superview] superview] superview] isUserInteractionEnabled](布尔) $8 = 是(lldb) p (BOOL)[[[[[0xa2e4800 superview] superview] superview] superview] isUserInteractionEnabled](布尔) $9 = 是(lldb) p (BOOL)[[[[[[0xa2e4800 superview] superview] superview] superview] superview] isUserInteractionEnabled](布尔)10 美元 = 否(lldb) po [[[[[0xa2e4800 superview] superview] superview] superview] superview](id) $11 = 0x00000000 <nil>

在这里,我发现直到根目录(UIWindow)的所有视图都从 isUserInteractionEnabled 返回 YES.窗口的 superview 为 nil.

I am attempting to place a UIButton in a UICollectionView supplementary view (footer). I have connected the UIButton using storyboards to a subclass of UICollectionViewCell and can change it's properties programmatically including the background image. However, when I wire up the touch up inside event of the button to a method, it does not fire. In fact, the button does not even appear to be responding visually to the user's touch. In troubleshooting, I attempted to add a UIButton to the collection view's header and see identical behavior. Adding the button to an unrelated view produces interaction effects when pressed.

Is there something special I need to do to implement a UIButton in a UICollection supplementary view?

解决方案

A supplementary view should be a UICollectionReusableView (or subclass thereof), not a UICollectionViewCell.

Anyway, if the button's not responding, the first thing to do is check that all of its ancestor views have userInteractionEnabled set to YES. Pause in the debugger once the button is on screen and do this:

(lldb) po [[UIApp keyWindow] recursiveDescription]

Find the button in the list and copy its address. Then you can check it and each superview. Example:

(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>

Here I found that all views up to the root (the UIWindow) return YES from isUserInteractionEnabled. The window's superview is nil.

这篇关于如何在 UICollection 补充视图中使用 UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆