87

I just started compiling up to iOS 11 and noticed that Apple now declared the property

var automaticallyAdjustsScrollViewInsets: Bool { get set }

as being deprecated:

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621372-automaticallyadjustsscrollviewin

enter image description here

Is there another property to fix this warning in iOS 11?

Will the default value stay true or how will this be handled in future?

Lepidopteron
  • 6,056
  • 5
  • 41
  • 53

4 Answers4

123

This code may help:

if #available(iOS 11.0, *) {
    scrollView.contentInsetAdjustmentBehavior = .never
} else {
    automaticallyAdjustsScrollViewInsets = false
}
liushuaikobe
  • 2,152
  • 1
  • 23
  • 26
tangkunyin
  • 1,383
  • 1
  • 8
  • 9
  • 1
    what is `scrollView` here? – Mando Dec 08 '17 at 10:38
  • 6
    @AlexeyStrakh scrollView is whatever `UIScrollView` that is having the insets adjusted. For example on a `UICollectionViewController` it would be `self.collectionView`, on a `UITableViewController` it would be `self.tableView`. – Gordon Tucker Dec 13 '17 at 17:45
  • Tested: This works for iOS 11 and iOS 10. Great thanks. – T.Coutlakis Feb 10 '18 at 11:13
  • This might work, but it doesn't make the warning go away. – turingtested Nov 17 '20 at 17:18
  • @GordonTucker I had a BaseViewController from UIViewController & there I was making `automaticallyAdjustsScrollViewInsets ` to false. Now the problem occurs as I will have to do it separately on each viewcontrollers. Is there any better way to handle this at one place? – Anirudha Mahale May 07 '21 at 15:20
104

The default for this property is now true. If you need to set this, you will need to set it in the scrollview that would host the viewController and set its property contentInsetAdjustmentBehavior. Below is an example:

scrollView.contentInsetAdjustmentBehavior = .automatic
totiDev
  • 5,189
  • 3
  • 30
  • 30
7

You can also set this in Interface Builder. Select your tableView or collectionView then select from the drop-down in the Size Inspector select .never for 'Content Insets Adjustment Behavior'

Size Inspector

Richard Hope
  • 251
  • 3
  • 6
2

For those who are wanting to adjust this from an objective-c perspective, here is the code:

self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever
Eli017
  • 31
  • 7