I'm new to the game so I've really only worked with iOS 11
and have recently acquired heartburn trying to figure out what Apple had in mind with their now-deprecated inset behaviors in iOS 10
. And Apple's documentation for UIScrollView
inset adjustment behavior is almost exclusively for iOS 11
.
Programmatic setup:
func setView() {
view = UIView()
view.frame = UIScreen.main.bounds
view.backgroundColor = UIColor.white
}
func addScrollView() {
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .always
}
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
scrollView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
}
func addFirstView() {
firstView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(firstView)
firstView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
firstView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
firstView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
firstView.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
...
func addLastView() {
lastView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(lastView)
lastView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
lastView.topAnchor.constraint(equalTo: someOtherView.bottomAnchor).isActive = true
lastView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
lastView.heightAnchor.constraint(equalToConstant: 100).isActive = true
lastView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
}
This setup works perfectly in iOS 11
(content insets are automatically adjusted) but in iOS 10
the content inset is not adjusted--the first view is flush with the view's top anchor (not the bottom of the status bar as it should be). automaticallyAdjustsScrollViewInsets
is true
by default. How do I enable automatic content inset adjustment behavior in iOS 10
?