I'm working with a UITableView. The table view has a HeaderView with a UIImageView that changes its height when the user scroll the table view.
In addition to this I also have a custom uiview for the section of the tableview .. My problem is that after setting the content Inset for the tableview, the section does not follow the scrolling of the table ..
Before Scroll
After Scroll
Surely it is a problem of content Inset, I did various tests but I could not find a solution ...
I would like the section of the tableView to follow the scroll and stop when it gets under the status bar (about 25 points of self.view.frame.origin.y)
Which path should I follow to solve my problem?
This is the code I am using
Register Xib for section header
- (void)viewDidLoad {
[super viewDidLoad];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
[_tableView registerNib:[UINib nibWithNibName:@"DashboardTopHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:dashboardTopHeader];
[self setupHeaderView];
}
Generate Section Header
#pragma mark - Dashboard Top Header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
DashboardTopHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:dashboardTopHeader];
return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 100;
}
Setup the Header View of TableView With Photo
-(void)setupHeaderView {
_headerView = [[UIView alloc] init];
_headerView = self.tableView.tableHeaderView;
_tableView.tableHeaderView = nil;
[_tableView addSubview:_headerView];
// 130 is Height of headerView
CGFloat newHeight = 130 - 50 /2;
_tableView.contentOffset = CGPointMake(0, -newHeight);
_tableView.contentInset = UIEdgeInsetsMake(newHeight, 0, 0, 0 );
[self updateHeaderView];
}
-(void)updateHeaderView {
CGFloat newHeight = 130 - 50 /2;
CGRect headerFrame = CGRectMake(0, -newHeight, self.tableView.frame.size.width, 130);
if (self.tableView.contentOffset.y < newHeight) {
headerFrame.origin.y = self.tableView.contentOffset.y;
headerFrame.size.height = - self.tableView.contentOffset.y + 50/2;
}
_headerView.frame = headerFrame;
}
Use TableView Delegate for scroll Content
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self updateHeaderView];
}