78

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 UISearchDisplayControlleretc. 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?

cojoj
  • 6,405
  • 4
  • 30
  • 52

8 Answers8

214

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 asvi

It must be unchecked so the default contentInset wouldn't be set after any change. After that it is just adding one-liner to viewDidLoad:

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

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: enter image description here
Also, you can achieve the same in code:

if #available(iOS 11.0, *) {
    scrollView.contentInsetAdjustmentBehavior = .never
} else {
    automaticallyAdjustsScrollViewInsets = false
}
cojoj
  • 6,405
  • 4
  • 30
  • 52
93

In Swift:

override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
      self.tableView.contentInset = UIEdgeInsets(top: 108, left: 0, bottom: 0, right: 0)
}
rmooney
  • 6,123
  • 3
  • 29
  • 29
  • Why not calling `super.viewDidLayoutSubviews()` ? – Alexander Perechnev Feb 10 '16 at 12:59
  • 2
    There is no need for it because the default implementation of viewDidLayoutSubviews does nothing. (Source: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/viewDidLayoutSubviews ) – rmooney Feb 10 '16 at 18:06
  • 8
    That's true Johanneke. It is best practice to call super, in case of architecture changes in your own application or changes to UIKit by Apple. – rmooney Apr 18 '16 at 16:32
11

automaticallyAdjustsScrollViewInsets is deprecated in iOS11 (and the accepted solution no longer works). use:

if #available(iOS 11.0, *) {
    scrollView.contentInsetAdjustmentBehavior = .never
} else {
    automaticallyAdjustsScrollViewInsets = false
}
Matan Guttman
  • 648
  • 8
  • 16
6

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 table

Fran Martin
  • 2,369
  • 22
  • 19
4

This is how it can be fixed easily through Storyboard (iOS 11 and Xcode 9.1):

Select Table View > Size Inspector > Content Insets: Never

Saeed Ir
  • 1,974
  • 2
  • 20
  • 22
2

After one hour of tests the only way that works 100% is this one:

-(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 is empty

Kappe
  • 9,217
  • 2
  • 29
  • 41
0

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

user3168555
  • 81
  • 1
  • 4
-3
      self.rx.viewDidAppearOnce
            .flatMapLatest { _ in RxKeyboard.instance.isHidden }
            .bind(onNext: { [unowned self] isHidden in
                guard !isHidden else { return }
                self.tableView.beginUpdates()
                self.tableView.contentInsetAdjustmentBehavior = .automatic
                self.tableView.endUpdates()
            })
            .disposed(by: self.disposeBag)
Hastler
  • 5
  • 5