简体   繁体   中英

How to add uiview and make it the self view center using autolayout and without setting frame or center property

How to add uiview and make it the self view center using autolayout and without setting frame or center property.

I know that we can set like this

view.center = window.center;

or

view.center = self.view.center;

but i want to set to the center of the view to self view center using autolayout.

You should be adding 4 constraints to your view then:

  • One for "center horizontally"
  • One for "center vertically"
  • One to set its height to a certain value
  • One to set its width to a certain value

You can create constraints in code like this, and add them to your view:

[NSLayoutConstraint constraintWithItem:label
                             attribute:NSLayoutAttributeCenterX 
                             relatedBy:NSLayoutRelationEqual 
                                toItem:tab
                             attribute:NSLayoutAttributeCenterX 
                            multiplier:1.0 
                              constant:0];

You can do this with storyboard you don't need to write any code. Use Size Inspector and remove all the line in Autosizing part.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM