Reputation: 994
I am new in iOS application development. In my application I need a login form at the centre of the screen. I put all the elements in a UIStackView
and that looks fine, but this UIStackView
not align centred in all screens. How to make this UIStackView
at the centre of screens.
Upvotes: 1
Views: 3513
Reputation: 1493
Add the Alignment Constraints as shown in the screenshot below and it will align the stackview to the center of the screen for all devices
Upvotes: 3
Reputation: 133
you can use viewDidLoad()
, and viewWillTransitionToSize()
to programmatically center the view. So Long as you have an IBOutlet bound to the stack view (called stackView in this example), this should work
stackView.frame.origin.x = view.frame.width/2 - stackView.frame.width/2
stackView.frame.origin.y = view.frame.height/2 - stackView.frame.width/2
just add this code in viewDidLoad
and viewWillTransitionToSize()
, depending if your program allows rotation, viewWillTransitionToSize()
is optional. If you do use viewWillTransitionToSize()
, there should be a parameter for size that you can replace view.frame
with.
viewDidLoad()
is called on startup, and viewWillTransitionToSize()
is called whenever the phone is rotated, if you want that functionality.
Upvotes: 0