Customize inline content in a UITableView

In my application, I have a UISearchBar under the UINavigationBar , so it is always displayed to the user. In this case, I had to set the contentInset with an additional 44px, so the UIScrollView will scroll under the UISearchBar (exactly the same as in Contacts.app) . And there would be no problem with static UITableView , but in my case I have to reload its contents, switch to UISearchDisplayController , etc. Therefore, when I call:

 [self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)]; 

Everything works until, for example, I pull to update ... (for this I use SSPullToRefresh ).

So my question is: How can I set the contentInset permanently, so I would not have to worry about any changes happening to the data inside the UITableView ?

+59
ios objective-c uitableview uiscrollview pull-to-refresh
Feb 25 '14 at 16:54
source share
7 answers

It was probably some kind of mistake of mine because I was busy with autorun and storyboard, but I found the answer.

You have to take care of this little man in the Attributes Inspector . asvi

It must be unchecked, so the default value of the contentInset will not be set after any changes. After that, it simply adds a single line to viewDidLoad :

 [self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)]; // 108 is only example 



iOS 11, Xcode 9 update

It seems that the previous solution is no longer correct when it comes to iOS 11 and Xcode 9 . automaticallyAdjustsScrollViewInsets deprecated and now, to achieve a similar effect, you need to go to the Inspector Size , where you can find this: enter image description here
Alternatively, you can do the same in code:

 if #available(iOS 11.0, *) { scrollView.contentInsetAdjustmentBehavior = .never } else { automaticallyAdjustsScrollViewInsets = false } 
+183
Feb 25 '14 at 18:00
source share

In Swift:

 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() self.tableView.contentInset = UIEdgeInsets(top: 108, left: 0, bottom: 0, right: 0) } 
+58
01 Oct '15 at 17:37
source share

automaticAdjustsScrollViewInsets is deprecated in iOS11 (and the decision taken no longer works). use:

 if #available(iOS 11.0, *) { scrollView.contentInsetAdjustmentBehavior = .never } else { automaticallyAdjustsScrollViewInsets = false } 
+8
Oct. 31 '17 at 15:23
source share

Add your code to the OfRowsInSection number [self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)]; . This way you will install your content. Always load data into a table.

+4
Feb 25 '14 at 17:02
source share

After one hour of testing, the only way that works 100% is as follows:

 -(void)hideSearchBar { if([self.tableSearchBar.text length]<=0 && !self.tableSearchBar.isFirstResponder) { self.tableView.contentOffset = CGPointMake(0, self.tableSearchBar.bounds.size.height); self.edgesForExtendedLayout = UIRectEdgeBottom; } } -(void)viewDidLayoutSubviews { [self hideSearchBar]; } 

with this approach you can always hide the search bar if it is empty

+2
Apr 10 '15 at 10:37
source share

Here's how to fix this easily through Storyboard (iOS 11 and Xcode 9.1):

Choose View Table> Size Inspector> Insert Content: Never

+2
Nov 02 '17 at 8:38 on
source share

Try setting tableFooterView tableView.tableFooterView = UIView (frame: CGRect (x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))

0
Apr 26 '19 at 6:28
source share



All Articles