James Chen
James Chen

Reputation: 407

How to stop a UIView from scrolling with tableview?

I have a UITableViewController and I put a UIView right under the navigation item and above the actual table. The problem that I have is that the view scrolls with the tableview.

How would I get it to behave exactly like the nav bar, and have the items in the tableview scroll behind it.

Rather than having the view scroll, it should remain in its position and have everything go behind it. Sorry for reiterating, but I've found thats necessary sometimes.

enter image description here

Upvotes: 1

Views: 1240

Answers (2)

rob mayoff
rob mayoff

Reputation: 385600

The view you're placing above the cell in the storyboard becomes the table view's tableHeaderView.

You can make the header view appear fixed by resetting its frame.origin to the table view's bounds.origin every time the table view lays out its subviews:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    UIView *header = self.tableView.tableHeaderView;
    CGRect frame = header.frame;
    frame.origin = self.tableView.bounds.origin;
    header.frame = frame;
}

Result:

demo of stationary table view header

Upvotes: 5

FredLoh
FredLoh

Reputation: 1842

Assuming you don't want the map view to move then you could set its user interaction to false.

Alternatively you could set the header of your tableView (if you only have one section) to the map view.

Upvotes: 0

Related Questions