UIScrollView's origin changes after popping ba

2019-01-16 08:38发布

I have a UIViewController subclass as a scene in the storyboard that contains a UIScrollView containing various subviews. One of the subviews is a UIButton which segues into another scene UIViewController subclass. When I come back from the view (pop the UIViewController off the navigation controller stack), I find that the scroll view's origin has somehow changed, although the contentsize and contentoffset seem correct.

What's also interesting is that the app has a tab bar, and when I tab away and back to that view, the scroll view is set back correctly with offset at (0, 0).

There is basically no code involved in this process, as it's pretty much all in the storyboard. As I am fairly new to using the storyboard, I figure I'm doing something wrong, although I don't know what. Any ideas as to what that may be? Perhaps sizing issues or constraints?

16条回答
贪生不怕死
2楼-- · 2019-01-16 09:38

Have you tried checking the Extend Edges Under Opaque Bars option? It can be found in your controller attributes inspector inside the storyboard.

It did the trick for me using XCode 9 iOS 11.

enter image description here

查看更多
别忘想泡老子
3楼-- · 2019-01-16 09:39

In iOS 11, I faced a similar issue where when I come back after popping a view controller, the tableview in the previous viewcontroller used to adjust its content inset automatically which resulted in tableview contents jumping from top abruptly. The following solution worked for me in iOS 11.

In Storyboard, select the tableview and go to Attributes inspector and uncheck "Automatic" for Row Height and Estimate fields. Also change the content insets from "Automatic" to "Never".

[Table view]

查看更多
太酷不给撩
4楼-- · 2019-01-16 09:40

I'm using a collectionView and I had a similar problem. For iOS 11: in the size inspector, there is "content inset". Set that to "Never". That solved the problem for me. I hope this helps someone.

Objective C:

if (@available(iOS 11, *)) {
   [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}

Swift:

if #available(iOS 11, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
}
查看更多
淡お忘
5楼-- · 2019-01-16 09:40

Turn off AutoLayout option for that xib otherwise.

查看更多
登录 后发表回答