0

I'm setting the tableHeaderView of a UITableView, but it is hiding the top cells of the table. In my case, I want to show tableview header I have used all possible solutions but not works. Please help.

UIView *headerView = self.headerView;

[headerView setNeedsLayout];
[headerView layoutIfNeeded];
CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

headerView.frame = ({
    CGRect headerFrame = headerView.frame;
    headerFrame.size.height = height;
    headerFrame;
});



NSLog(@"Header Frame : %@",headerView);
self.reviewsTableView.tableHeaderView = headerView;

View debugger screenshot

beyowulf
  • 15,101
  • 2
  • 34
  • 40
Mukesh Lokare
  • 2,159
  • 26
  • 38

3 Answers3

1

try writing the below lines at the end [self.tableView setNeedsUpdateConstraints]; [self.tableView updateConstraintsIfNeeded];

if you set headerview using storyboard then it will become easy

Deepak Kumar
  • 175
  • 9
0

Don't do it in viewDidLoad. Instead do it in viewWillLayoutSubviews. This works for me.

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [_headerWrapper setNeedsLayout];
    [_headerWrapper layoutIfNeeded];
    CGFloat height = [_headerWrapper systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect headerFrame = _headerWrapper.frame;
    headerFrame.size.height = height;
    _headerWrapper.frame = headerFrame;
    _tableView.tableHeaderView = _headerWrapper;
}
Kunal
  • 227
  • 3
  • 9
  • Then check that the height that your setting is correct or not. Also, check your constraints in the headerview because then only will UILayoutFittingCompressedSize return the correct dynamic height. – Kunal Aug 03 '16 at 13:12
  • It seems my constraint was incorrect. Its worked for me UIView *headerView = [[UIView alloc]initWithFrame:self.headerView.frame]; [headerView addSubview:self.headerView]; self.reviewsTableView.tableHeaderView = headerView; – Mukesh Lokare Aug 03 '16 at 13:17
0

I was work around this issue, In my case real issue was with constraints in the storyboard.

You have to reset constraint from the storyboard.

Or if you are setting it programmatically then you needed to call updateConstraintsIfNeeded method after setting of constraints to the widget.