Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a UIButton in a UICollection supplementary view?

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?

like image 445
markdorison Avatar asked Nov 26 '12 19:11

markdorison


People also ask

How to add section header in collection view?

There are no section headers in the UICollectionView. So for your first task, you'll add a new section header using the search text as the section title. To display this section header, you'll use UICollectionReusableView .


1 Answers

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.

like image 91
rob mayoff Avatar answered Oct 23 '22 01:10

rob mayoff