Is it possible to disable floating headers in UITableView with UITableViewStylePlain?
A probably easier way to achieve this:
Objective-C:
CGFloat dummyViewHeight = 40;
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyViewHeight)];
self.tableView.tableHeaderView = dummyView;
self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0);
Swift:
let dummyViewHeight = CGFloat(40)
self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: dummyViewHeight))
self.tableView.contentInset = UIEdgeInsets(top: -dummyViewHeight, left: 0, bottom: 0, right: 0)
Section headers will now scroll just like any regular cell.
(For who ever got here due to wrong table style) Change Table style from plain to grouped, via the attributes inspector, or via code:
let tableView = UITableView(frame: .zero, style: .grouped)
WARNING: this solution implements a reserved API method. This could prevent the app from being approved by Apple for distribution on the AppStore.
I've described the private methods that turns of section headers floating in my blog
Basically, you just need to subclass UITableView
and return NO
in two of its methods:
- (BOOL)allowsHeaderViewsToFloat;
- (BOOL)allowsFooterViewsToFloat;