In my app I have a UISearchBar
under UINavigationBar
so it is always visible to user. In that case I had to set contentInset
with extra 44px so the UIScrollView
will be scrolling under UISearchBar
(exactly like in Contacts.app). And there would be no problem with static UITableView
's but in my case I have to reload it's contents, switch to UISearchDisplayController
etc. So when I call:
[self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)];
Everything works until e.g. I pull to refresh... (for this I use SSPullToRefresh
).
So my question is: How can I set contentInset
permanently so I wouldn't have to worry about any changes happening to data inside UITableView
?
In Swift:
Add in numberOfRowsInSection your code
[self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)];
. So you will set your contentInset always you reload data in your tableautomaticallyAdjustsScrollViewInsets is deprecated in iOS11 (and the accepted solution no longer works). use:
After one hour of tests the only way that works 100% is this one:
with this approach you can always hide the search bar if is empty
Probably it was some sort of my mistake because of me messing with autolayouts and storyboard but I found an answer.
You have to take care of this little guy in View Controller's Attribute Inspector
It must be unchecked so the default
contentInset
wouldn't be set after any change. After that it is just adding one-liner toviewDidLoad
:iOS 11, Xcode 9 update
Looks like the previous solution is no longer a correct one if it comes to iOS 11 and Xcode 9.
automaticallyAdjustsScrollViewInsets
has been deprecated and right now to achieve similar effect you have to go to Size Inspector where you can find this:Also, you can achieve the same in code:
This is how it can be fixed easily through Storyboard (iOS 11 and Xcode 9.1):
Select Table View > Size Inspector > Content Insets: Never