15

I've got a button that I'm adding as a subview of a table view's tableHeaderView. The button appears fine, and tap-and-holding on it works intermittently - for the most part, though, it's unresponsive. I've tried adding it as a subview of the table itself; the effect is about the same. I thought the problem might be with the scroll view's touch interception, but disabling scrolling on the table has no effect either.

Am I doing something wrong? Has anyone else encountered this?

edit - to clarify, I'm talking about the main table header, not a section header, in a grouped-style table; think basically modeled after the "Contact" screen.

Ryan Townshend
  • 2,404
  • 21
  • 36
Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • I was experiencing this exact issue, but the accepted answer did not work for me. The [answer to this question](http://stackoverflow.com/questions/19256996/uibutton-not-showing-highlight-on-tap-in-ios7/19299451#19299451) fixed the issue for me perfectly. – Paul Evans Jan 16 '14 at 03:46

10 Answers10

24

I had the same problem. In my case I had a container view instantiated in IB (that was applied as the table view header in code), with a UIImageView occupying the entire frame of that container. The misbehaving button resided in the image view.

Turns out I needed to have sizing struts in IB set as follows...

Container View: exterior anchors all on, interior resizing - all off

Sub Image View: all struts on (interior and exterior)

I had several different table views, all using header views. Some would respond to touch events correctly, some were flaky. This solved my problem

Danny Hall
  • 256
  • 2
  • 3
  • 3
    +1. This worked for me. I'm not using IB for this, so I had to programmatically set the struts with `-setAutoresizingMask:`, and I found it only necessary on the container. The inner image control didn't need anything. Thanks! Would never have stumbled on this by accident. – Ben Zotto Mar 03 '10 at 13:57
  • After hours of bashing my head, this worked for me. Thank you. – Josh Bernfeld Jan 19 '13 at 10:51
  • 1
    Indeed. In my case I had some UITextFields in the header which would bring up the keyboard. I was resizing my table view to accomodate the keyboard. Everything looked correct visually and the table scrolled exactly as desired. However none of the controls in the table header were responsive one I changed the size of the table view. My container view did have the interior scaling on. Once I turned it off, all worked! Thanks. – Craig B May 04 '13 at 01:25
  • Thank you. It seems the key here is that "interior resizing" of the container view must be disabled. – Pochi Nov 07 '19 at 05:56
11

I had a similar problem - a textfield and button inside a view set as the table header view which would not respond to touch events. setAutoResizing programmatically worked for me.

My controller extends UITableViewController, and viewDidLoad looks like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    MYCustomWidget *headerView = [[[NSBundle mainBundle] 
               loadNibNamed:@"MYCustomWidgetView" owner:self options:nil] 
               objectAtIndex:0];

    [headerView setAutoresizingMask:UIViewAutoresizingNone];

    self.tableView.tableHeaderView = headerView;
}

('MYCustomWidget' extends UIView, and 'MYCustomWidgetView' is a XIB file).

8

I completely disagree with Wisequark -- there's absolutely nothing wrong with putting a button in the tableHeaderView, and including one would not risk your app being rejected from the app store. The tableHeaderView is designed to be an arbitrary view containing whatever elements you choose.

As far as your issue, it could be that you've got a view obscuring your button, or, it may simply be a bug that should be reported to Apple.

August
  • 12,139
  • 3
  • 29
  • 30
4

Strangely enough, but the table header view is apparently resized incorrectly. I use auto layout, so autoresizing mask was not an option for me. After inspecting my view hierarchy: enter image description here

and noticed that my custom header view had incorrect height, so only less then half of it was tappable (see highlighted view):

enter image description here

Manual updating of its height fixed the problem:

- (void)viewDidLayoutSubviews {
    CGRect frame = self.tableView.tableHeaderView.frame;
    frame.size.height = 116.0;
    self.tableView.tableHeaderView.frame = frame;
}

enter image description here

Also, the table view header height can become invalid after the orientation is changed. This problem also can be fixed with the provided solution.

Davyd Geyl
  • 4,578
  • 1
  • 28
  • 35
2

My situation was similar to Danny Hall's (the table header view was a UIImageView, and there was a UIButton which was a subview of the UIImageView). In my case, the problem appears to have been caused by the button being a subview of the image view. What worked for me was creating a UIView "containing" view, such that both the image view as well as the button were subviews of the "containing" view. strange.

cell
  • 151
  • 1
  • 5
  • Yeah, UIImageView has `userInteractionEnabled` set to `NO` by default, so a button as a subview of an image view won’t respond to touches. I’m pretty sure this was a different issue. Thanks for the suggestion, though. :) – Noah Witherspoon Jan 10 '12 at 23:46
1

tableHeaderView has 0 height while it is processing draw in UITableView

use this UIView subclass to set the strong constant height and ignore UITableView processing

#import <UIKit/UIKit.h>                                                
@interface CustomHeaderCell : UIView

@end  

//-----

@import "CustomHeaderCell.h"

@implementation CustomHeaderCell


-(void)setFrame:(CGRect)frame {
    frame.size.height = 43; // !!! constant height
    [super setFrame:frame];
}

@end
1

I have the same problem UIButtons actions not working in UITableView's header view. First i tried setAutoresizingMask to .None which not works then after reading the answers of @Davyd and @Alexey i realise that i did not set the height of headers view then i set it like:-

self.tablevwMain.tableHeaderView?.frame = CGRect(x: 0, y: 0, width: width_view, height: your_height)

And all UIButton'sinside UITableView's header view works correctly.

Ravi Sharma
  • 975
  • 11
  • 29
0

For me UIControl like UIButtons on headers only worked if I add it to the cell's contentView

addSubview(stackView) //Does not work
contentView.addSubview(stackView) //Works
Samuel Folledo
  • 442
  • 6
  • 15
-2

Don't forget to set the footer height in:

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
Pang
  • 9,564
  • 146
  • 81
  • 122
mskw
  • 10,063
  • 9
  • 42
  • 64
-9

You should consider that this is not the intended sue of the headerView and that an implementation such as that might result in rejection from the AppStore as a result of a HIG violation. Given that the dimensions of a header are intended to be small, it is probably better to consider a restructuring of your view. Having said that, there is no easy way to do it short of hand detecting touch events and determining the geometry yourself, then executing a selector based on the geometry - in short, rolling your own button class.

wisequark
  • 3,288
  • 20
  • 12
  • I can't find anything in the HIG prohibiting the use of buttons in table (as opposed to section) header/footer views; the Contacts app attaches buttons to the table footer without issue, and Apple's "HeaderFooter" sample does something similar (though it exhibits the same problem). Radar, maybe? – Noah Witherspoon Nov 18 '08 at 00:03
  • More likely than not I would suggest filing a bug. – wisequark Nov 18 '08 at 17:29