UIButton in a UITableView header ignores most touches

前端 未结 9 2340
盖世英雄少女心
盖世英雄少女心 2020-12-28 18:58

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 p

相关标签:
9条回答
  • 2020-12-28 19:07

    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:

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

    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;
    }
    

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

    0 讨论(0)
  • 2020-12-28 19:10

    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

    0 讨论(0)
  • 2020-12-28 19:13

    Don't forget to set the footer height in:

    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    0 讨论(0)
  • 2020-12-28 19:16

    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.

    0 讨论(0)
  • 2020-12-28 19:19

    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
    
    0 讨论(0)
  • 2020-12-28 19:20

    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.

    0 讨论(0)
提交回复
热议问题