Auryn
Auryn

Reputation: 1176

UITableView Section Header Stop Position

Is it possible to give the section header in ui tableview an offset to stop programmatically?

So, that the section header stop 100 px from top?

Upvotes: 3

Views: 2874

Answers (3)

Megha_Singh
Megha_Singh

Reputation: 100

You can try this one

CGFloat newViewHeight = 40;

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, newViewHeight)];

self.tableView.tableHeaderView = newView;
self.tableView.contentInset = UIEdgeInsetsMake(-newViewHeight, 0, 0, 0);

Section headers will now scroll just like any regular cell.

Upvotes: 1

ANE
ANE

Reputation: 243

I think this should work:

override func scrollViewDidScroll(_ scrollView: UIScrollView)
{
    super.scrollViewDidScroll(scrollView)
    let inset: CGFloat = 73
    if scrollView.contentOffset.y < inset && scrollView.contentOffset.y > 0 {
        scrollView.contentInset = UIEdgeInsets(top: scrollView.contentOffset.y, left: 0, bottom: 0, right: 0)
    } else {
        if scrollView.contentOffset.y < 0 {
            scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        } else {
            scrollView.contentInset = UIEdgeInsets(top: inset, left: 0, bottom: 0, right: 0)
        }
    }
}

Upvotes: 4

Andrew Romanov
Andrew Romanov

Reputation: 5066

You can use property of UIScrollView: contentInset. And style for a table view UITableViewStyleGrouped (.group in Swift). This is my example of such UI: enter image description here

If you want to avoid header moving, set Bounce Vertical property to NO.

Upvotes: 0

Related Questions