Reputation: 4880
I have a basic UIScrollView set up inside a UIViewController, which lives in a UINavigationController. I'm using autolayout to make the scroll view fill the view controller, and I have a single content view inside the scroll view that holds some other views. As far as I can tell, this is set up correctly.
When the view controller is first loaded (i.e. pushed onto the navigation stack) the content of the scroll view and the scroll bar appears behind the navigation bar. However, when I rotate the device to landscape, then back to portrait, the content moves to its correct position below the nav bar.
I do want the content to scroll behind the navigation bar, but it should start below it. I have automaticallyAdjustsScrollViewInsets
turned on in IB for my UIViewController. Like I said, I get the correct behavior after rotating the device, just not on initial load.
Any ideas why this might be happening and what I could do to fix it? Thanks!
Upvotes: 2
Views: 5131
Reputation: 2479
In my case problem was in my viewWillAppear
method.
Make sure that a super call is in first line of overrides method body:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// your codes will be here
}
Upvotes: 0
Reputation: 4880
Solved this by turning off automaticallyAdjustsScrollViewInsets
and doing it manually in viewWillLayoutSubviews
, as below.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let insets = UIEdgeInsets(top: self.topLayoutGuide.length, left: 0, bottom: 0, right: 0)
scrollView.contentInset = insets
scrollView.scrollIndicatorInsets = insets
}
Not sure what the original problem was, but this works for now. However, if anyone has insight into the original issue, please let me know!
Upvotes: 4