Xcode how to make flexible constraint in Autolayout

蹲街弑〆低调 提交于 2019-12-23 06:23:43

问题


hello everyone I've get stuck in Auto-layout issues

I have login storyboard that contains three Items Image-view , Stack-view , Button like the first image below

I've tried to add constraints using two ways:

The first way I've added "pinned" constraint to Image-view as shown in Photo1 below but when I've changed to landscape the Image was disappeared as the second photo below.
please notice I haven't got any warnings

The second Way I've tried by wrap the Image-view using view then i added "Pinned" constraint to view finally i added fixed width & height to Image-view and Horizontal & vertical in container but I've got this result as third and fourth photo

what should i do to make my Image-view Adaptive for all Screens ?

thanks

Photo1 Photo2 Photo3 Photo4


回答1:


As when changing to landscape screen height changing a lot. the problem with your design with Autolayout is it's not adaptive with landscape and portrait mode because you are give fixed hight and width to your layout.

to make your image-view Adaptive for all screens give your image-view height to superview height in percentage so when in landscape mode height will change your image-view will still gets the height from superview height in exact same percentage.

have a look at this demo hope this will help. Autolayout

  • First drop UIImageView into ViewController.
  • Give it a top constraint and Align it horizontally to it's SuperView. As now it's only required height and width.
  • Then Ctrl drag from ImageView to itself and select aspect ratio. give a aspect ration 1:1 (it's just telling storyboard if any of height or width is available i want the other to be the same like if width is given then height should be the same as width, vice versa for height) as you may want perfect square shape for your profile ImageView.

  • Now Ctrl drag from ImageView to SuperView and select Equal height.

  • As now your ImageView got the height to SuperView Height. give your ImageView proportional height (percentage, Aspect Ration or Multipiler) to SuperView how much you wanted to give like so.

At the end your UIImageView will get the height proportional to SuperView height, vice versa for width.



来源:https://stackoverflow.com/questions/49763145/xcode-how-to-make-flexible-constraint-in-autolayout

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