I have a UITableView with its backgroundView property set to a UIImageView showing a background image. I’m then drawing some cells on top of the image and everything’s working great.
Now I’m trying to display a bar at the top of the screen. To prevent the bar from covering the top rows of the table view, I’m setting its contentInset property to UIEdgeInsetsMake([topBar frame].size.height, 0, 0, 0).
The problem is, changing the contentInset appears to also push the background image view down, so there’s a weird white gap at the top of my table. Weirder still, as soon as I scroll the table view, it fixes itself. Anyone have any idea what’s going on?
If I change the table’s frame as opposed to its contentInset to make room for the top bar, everything works fine, but the background image of the table view is also shifted down, which makes the view appear inconsistent compared to other table views in the app that don’t have a top bar.
This is how I’m setting the table’s background in the table’s init method:
_backgroundImageView = [[UIImageView alloc] initWithFrame:backgroundRect];
[_backgroundImageView setImage:...];
[_backgroundImageView setContentMode:UIViewContentModeBottom];
[self setBackgroundView:_backgroundImageView];
And in the view controller’s viewDidLoad
[_tableView setContentInset:UIEdgeInsetsMake([_topBarView frame].size.height, 0, 0, 0)];
Edit: Well, moving the setContentInset out of viewDidLoad into viewDidAppear: makes it work correctly, except as the view is shown the content inset is wrong, and then it snaps into place once the view appears. Moving the line to viewWillAppear: instead makes it behave as before. Guess something weird is happening between viewWillAppear: and viewDidAppear: that I’m not aware of…
You might just make your table view transparent and put an image view behind it. The proper way to avoid other views (the top bar in your case) is to set the frame property.