4

Ok I feel like an idiot that I can't get this working, and none of the other answers on SO are getting me anywhere.

I have a very simple UIView that I have defined in a .xib and am loading as the tableHeaderView of my UITableViewController:

UIView* headerView = [[NSBundle mainBundle] loadNibNamed:@"ProfileHeaderView" owner:self options:nil][0];
self.tableView.tableHeaderView = headerView;

The file's owner of the header .xib is set to my controller class, and so far this much works fine. My outlets are connecting properly and I can set my labels to their correct values, etc.

But, when I added a UIButton to my headerView, it is not responding to touches no matter what I try.

I've confirmed that the frames of the header and of the button are correct and both have userInteractionEnabled as YES.

I cannot for the life of me figure out why this isn't working. I am really not doing anything tricky here that I can think of.

Here's some debug info:

po self.tableView.tableHeaderView
<UIView: 0x17ef5420; frame = (0 0; 320 194); autoresize = W+H; layer = <CALayer: 0x17eda4a0>>

po self.loginButton
<UIButton: 0x17ec1630; frame = (80 164; 160 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x17eec760>>

Update: I discovered some new evidence. The above were logged inside viewDidLoad, and the frames have their correct values. However, if I set a breakpoint in tableView:didSelectRowAtIndexPath:, I notice that somehow my header view frame height has changed to 130 px!

po self.profileHeaderView
<UIView: 0x15eb6790; frame = (0 0; 320 130); autoresize = W+H; layer = <CALayer: 0x15eb67f0>>

I don't know what is causing this! Of course now that I know this, if I explicitly set the frame's height back to 194 in viewDidAppear: it works fine. But I would obviously like to fix whatever is changing my frame in the first place.

The header still draws properly, and is the correct height (194 px) when it is rendered, but for some reason its frame is getting changed at some point, which is causing the button to fall outside of its frame and thus not receive touches.

What would be changing the header's frame like this? I don't have any constraints set up on it at all.

devios1
  • 36,899
  • 45
  • 162
  • 260
  • Do you have a target/selector for your button? How did you add the button, IB? You didn't post any code actually relevant to the button receiving touches. – JDM Apr 01 '15 at 00:29
  • Yes the button was added to the xib for the header view and wired up via an outlet action. Pretty typical. But the button won't even highlight when touched so it's not even making it that far. – devios1 Apr 01 '15 at 02:44
  • 1
    Could you also print `po self.loginButton.superview` – Burhanuddin Sunelwala Apr 01 '15 at 04:18
  • I'm having the same issue. Did you figure it out? – flohei Nov 26 '15 at 19:10
  • 1
    @flohei My solution was to give up trying to do this in a separate .xib file and to just add it as a table view header in the storyboard by dragging a UIView onto the table view. – devios1 Nov 26 '15 at 19:13
  • Yeah, I tried that as well and it works for me, too. Though this is not my preferred solution. – flohei Nov 27 '15 at 07:51

2 Answers2

5

Try using this ,

    [button sendActionsForControlEvents:UIControlEventTouchUpInside];

If this properly calls your target / selector then the problem is with your view hierarchy. If not then problem is that you haven't properly coded the target / selector for the button.

Jaffer Sheriff
  • 1,444
  • 13
  • 33
  • This works. It must be something to do with the view hierarchy. Any pointers what to check for? Everything seems completely in order. – devios1 Apr 01 '15 at 16:38
  • Pls see my update. I discovered what's wrong with the view hierarchy, but now I need to figure out what is causing it. – devios1 Apr 01 '15 at 16:48
  • Maybe you are confused with tableHeaderView and section header view. I tried the same and i was clearly able to call button action during button click. See link https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instp/UITableView/tableHeaderView. Post your code so that we can easily debug it. – Jaffer Sheriff Apr 02 '15 at 06:11
0

maybe you can set your button backgroundColor is red or another color.and you can write this 'self bringSubviewToFront:xx',that means you set your button to the topView.