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

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

问题描述

我试图在UICollectionView补充视图(页脚)中放置UIButton。我已经将UIButton使用storyboards连接到UICollectionViewCell的子类,并可以通过编程方式更改其属性,包括背景图像。然而,当我把触摸上面的按钮事件的方法,它不会发射。事实上,该按钮甚至看起来不是对用户的触摸可视地响应。在故障排除过程中,我试图添加一个UIButton到集合视图的头,并看到相同的行为。

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.

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

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

推荐答案

补充视图应该是 UICollectionReusableView (或其子类) code> UICollectionViewCell 。

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

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

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]

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

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>

在这里,我发现所有的视图,直到根( UIWindow )return YES isUserInteractionEnabled 。窗口的superview是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天全站免登陆