How can I use UIButton in an optional UICollection?

I am trying to place a UIButton in an optional UICollectionView (footer). I connected UIButton using a storyboard to a subclass of UICollectionViewCell and can change its properties programmatically, including a background image. However, when I connect the button's touch signal to the method, it does not work. In fact, the button does not even seem visually touchy to the user. When troubleshooting, I tried adding a UIButton to the collection view header and seeing identical behavior. Adding a button to an unrelated view causes interaction effects when clicked.

Is there anything special about implementing UIButton in an optional UICollection?

+8
ios objective-c cocoa-touch ios6 uicollectionview
source share
2 answers

An additional view should be a UICollectionReusableView (or its subclass), and not a UICollectionViewCell .

In any case, if the button does not respond, the first thing to do is verify that all its ancestor views have userInteractionEnabled set to YES . Pause in the debugger after pressing a button on the screen and do the following:

 (lldb) po [[UIApp keyWindow] recursiveDescription] 

Find the button in the list and copy its address. You can then check this and each view. 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 to the root ( UIWindow ) return YES from isUserInteractionEnabled . The supervisor window is zero.

+16
source share

I have a custom button that subclasses UIControl and places it inside a UICollectionReusableView . To make it work, I made all the subheadings of the control, and its subviews subzones do not interact with the user.

 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 subviews. self.disableUserInteractionInView(self.contentView) 

From the moment this code is added, all .TouchUpInside event .TouchUpInside will fire and the control will visually stand out when pressed.

0
source share

All Articles