iOSdev
iOSdev

Reputation: 563

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.

Upvotes: 0

Views: 471

Answers (2)

NevzatR
NevzatR

Reputation: 165

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.

enter image description here

Upvotes: 0

Berk
Berk

Reputation: 367

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];

Upvotes: 1

Related Questions