Center UIView on screen using Auto Layout programmatically when the view is located at the top of the view hierarchy

StackOverflow https://stackoverflow.com/questions/23190404

Question

I have implemented a popup UIView which I add to the topmost window via [[[[UIApplication sharedApplication] windows] lastObject] addSubview:popupView], so it will appear on top of everything even the keyboard. I need to ensure this popup view that I programmatically created will always remain centered on screen. I was attempting to add auto layout constraints, but it doesn't like the fact I'm trying to align with the topmost window. Could you let me know how I could accomplish this? Thank you.

This is what I have implemented, which will generate a (nicely detailed) error that states 'The view hierarchy is not prepared for the constraint ... the constraint's items must be descendants of that view':

    [popupView setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSLayoutConstraint *cn = nil;
    cn = [NSLayoutConstraint constraintWithItem:popupView
                                      attribute:NSLayoutAttributeCenterX
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:[[[UIApplication sharedApplication] windows] lastObject]
                                      attribute:NSLayoutAttributeCenterX
                                     multiplier:1.0
                                       constant:0];
    [popupView addConstraint:cn];
    cn = [NSLayoutConstraint constraintWithItem:popupView
                                      attribute:NSLayoutAttributeCenterY
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:[[[UIApplication sharedApplication] windows] lastObject]
                                      attribute:NSLayoutAttributeCenterY
                                     multiplier:1.0
                                       constant:0];
    [popupView addConstraint:cn];
    cn = [NSLayoutConstraint constraintWithItem:popupView
                                      attribute:NSLayoutAttributeHeight
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:nil
                                      attribute:NSLayoutAttributeNotAnAttribute
                                     multiplier:1
                                       constant:blockSize.height];
    [popupView addConstraint:cn];
    cn = [NSLayoutConstraint constraintWithItem:popupView
                                      attribute:NSLayoutAttributeWidth
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:nil
                                      attribute:NSLayoutAttributeNotAnAttribute
                                     multiplier:1
                                       constant:blockSize.width];
    [popupView addConstraint: cn];
Was it helpful?

Solution 2

It's fine to add the height and width constraints to popupView, but since popupView is a subview of the window, the centering constraints should be added to the window, not the popupView (and you need to add the popupView as a subview first, before you add the constraints).

OTHER TIPS

Don't go sticking your view into a window that wasn't set up specifically to hold your views. You don't know what the real owner of that window might do, now or in a future version of iOS.

Create your own window and set its windowLevel high enough to be above the keyboard. Give the new window its own root view controller (so it will handle different orientations properly) and center your popup view inside that root view controller's view.

You will find lots of useful information in the answers to this question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top