UIScrollViews subviews not resizing themselves on Portrait to Landscape Orientation Change

末鹿安然 提交于 2019-12-04 19:41:35

try this : this problem will solve by constraints only. I created a project and by using correct constraints its working fine for me ,

here I am going to show you how to add correct constraint one by one :

  1. add scrollview and add constraints for scrollview only like in the screenshots :

2. now add search bar and add constraints for search bar too, 
here below you can find out complete constraints for scrollview and search together:

Scrollviews don't play very well with constraints. Here's an approach using constraints and Interface Builder that worked for me for having a vertically scrolling scrollview that doesn't require any manual or content size finagling and works with rotation:

  • Scrollview in a view controller positioned with constraints
  • A container (UIView) that is a subview of the scrollview positioned with constraints pinning each edge to the superview
  • Add all your additional subviews to the container view instead. Use constraints. Exceed the height of the scrollview to get a vertically scrolling scrollview.

There is one trick however to get rotation resizing to work automagically:

  • You have to add an additional constraint to the container view to match its width to the UIScrollview's width. It's strange that you have to do this, since pinning each side to the scrollview should do the trick, but alas it won't work with rotation if you don't add the additional constraint.

Here's what I mean:

You could use UIViewAutoresizing mask (autoresizingMask property) of your subviews.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!