RyJ RyJ - 4 months ago 25
iOS Question

iOS: Keep view centered on screen during rotation

If you look at the default alert view, when presented, it stays in exactly the same location during device rotation. It appears to rotate from its own center.

I've made a custom alert view and would like the same behavior. I have a container view which includes a background view (to dim out the screen some) and the alert view. When I rotate, the alert view jumps to what will be the new center after the rotation, then animates into place. How can I get it to rotate from its own center?

Answer

The problem is merely that your constraints positioning the alert view are incorrect. If they are correct, centering will happen automatically and will remain in place during rotation.

For example:

v.autoresizingMask = .FlexibleTopMargin | .FlexibleBottomMargin | .FlexibleLeftMargin | .FlexibleRightMargin
v.setTranslatesAutoresizingMaskIntoConstraints(true)
Comments