Center UI elements in view with autolayout

浪子不回头ぞ 提交于 2019-12-12 05:54:50

问题


How do you use Autolayout to make a UI Element centered left-to-right when in both landscape and portrait mode? Can you have variables in autolayout?


回答1:


If you are using Interface builder to create & set position of your view, Select your View & do as the screenshot suggests below.

After that, click on "Add constraint" & you are done. This is just to set your view in horizontal centre. But you have to still add some more constraints. They are:

  1. Height
  2. Width
  3. Distance from top/bottom layout guide/view.

Let me know if you don't know how to do this.




回答2:


set constraints both vertical and horizontal

more understand autolayout easily follow this link

http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2

output portait

output landscape




回答3:


in your xib ,you should add 3 constraints :

  1. Hirizontal Center in Container;
  2. Width ;
  3. Height ;


来源:https://stackoverflow.com/questions/21793542/center-ui-elements-in-view-with-autolayout

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