iosobjective-cuitableviewuikituitableviewsectionheader

How to get a custom section header's background to respond to scrolling in table view


In iOS 15 section headers views will change to a gray blurred background as you scroll the table view.

How do you mimic or adopt this same behavior for custom section header views? I'm returning a label as my custom view.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [UILabel new];
    // ...
    return label;
}

Solution

  • You can put your label inside a custom subclass of UITableViewHeaderFooterView which does it automatically for you,

    Apologies, I only know Swift, but here are the steps explained:

    1. Create your custom header class: enter image description here

    2. Next, register it to your tableview in viewDidLoad: enter image description here

    3. And, finally use it inside your viewForHeaderInSection: enter image description here

    Basically, create a custom subclass of UITableViewHeaderFooterView (in code or using nib), configure and add your label as a subview to the header's contentView, register the custom header to your tableview, and then call it in the delegate method