UIButton in a UITableView header ignores most touches
Asked Answered
M

10

16

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.

Milo answered 17/11, 2008 at 23:7 Comment(1)
I was experiencing this exact issue, but the accepted answer did not work for me. The answer to this question fixed the issue for me perfectly.Mylan
L
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

Laugh answered 1/1, 2010 at 18:25 Comment(4)
+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.Cinematograph
After hours of bashing my head, this worked for me. Thank you.Halfslip
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.Oberammergau
Thank you. It seems the key here is that "interior resizing" of the container view must be disabled.Lura
S
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).

Selfexistent answered 25/10, 2011 at 23:44 Comment(2)
setting the AutoresizeMask did it for me.Phira
How about in swift 4?Ladoga
Z
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.

Zusman answered 19/11, 2008 at 5:14 Comment(0)
D
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.

Dichroic answered 30/9, 2015 at 12:15 Comment(0)
R
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.

Rip answered 10/1, 2012 at 23:42 Comment(1)
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. :)Milo
D
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
Dawkins answered 25/10, 2013 at 12:53 Comment(0)
E
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.

Effects answered 17/4, 2017 at 8:1 Comment(0)
J
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
Juliusjullundur answered 4/5, 2022 at 22:47 Comment(0)
E
-2

Don't forget to set the footer height in:

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
Emmett answered 16/10, 2015 at 19:7 Comment(0)
D
-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.

Docia answered 17/11, 2008 at 23:43 Comment(2)
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?Milo
More likely than not I would suggest filing a bug.Docia

© 2022 - 2024 — McMap. All rights reserved.