How to hide first section header in UITableView (grouped style)
I have a workaround that seems reasonably clean to me. So I'm answering my own question.
Since 0 as the first section header's height doesn't work, I return 1. Then I use the contentInset to hide that height underneath the navigation bar.
Objective-C:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return 1.0f;
return 32.0f;
}
- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return nil;
} else {
// return some string here ...
}
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.tableView.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
}
Swift:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return section == 0 ? 1.0 : 32
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.contentInset = UIEdgeInsets(top: -1, left: 0, bottom: 0, right: 0)
}
How to remove section header separator in iOS 15
Option 1:
Maybe by using UITableViewCellSeparatorStyleNone
with the table view and replacing the system background view of the cell with a custom view which only features a bottom line?
Option 2: Using hint from https://developer.apple.com/forums/thread/684706
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000 // only Xcode 13+ needs and can compile this
if (@available(iOS 15.0, *)) {
[self.tableview setSectionHeaderTopPadding:0.0f];
}
#endif
}
How to change height of grouped UITableView header?
It appears that I can't set a table header view with height of 0. I ended up doing the following:
- (void)viewWillAppear:(BOOL)animated{
CGRect frame = self.tableView.tableHeaderView.frame;
frame.size.height = 1;
UIView *headerView = [[UIView alloc] initWithFrame:frame];
[self.tableView setTableHeaderView:headerView];
}
How to dynamically change UITableView section header with scrolling. Or how to add a button to table section header?
Creating a simple subclass of UITableViewHeaderFooterView
helped. It kept all the standard behavior and visuals, while I just added a button on top.
Related Topics
Swift: Testing Optionals for Nil
Convert JSON String to JSON Object in Swift 4
How to Convert an iPhone Osstatus Code to Something Useful
Xcode Is Creating Generic Xcode Archive Instead of iOS App Archive
Hide Dots from Uipageviewcontroller
Cannot Create Outlet Connections to Subviews in Interface Builder (Xcode 5)
How to Rotate an Uiimageview by 20 Degrees
Get Email and Name Facebook Sdk V4.4.0 Swift
Request Permissions Again After User Denies Location Services
Nsurlsession "Http Load Failed Kcfstreamerrordomainssl, -9813 ; Self Signing Certificate
Swift Regex: Does a String Match a Pattern
How to Get Indexpath in Prepareforsegue
How Does One Print All Wkwebview on and Offscreen Content Osx and iOS
When Will Applicationwillterminate Be Called
How to Set Uiviewcontroller "Extend Edges" Properties
Using Custom Fonts in Interface Builder
Adding Frameworks to Project in Xcode 5 and Having *Relative* Paths Added