[英]Center UIView on screen using Auto Layout programmatically when the view is located at the top of the view hierarchy
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. 我实现了一个弹出UIView,它通过[[[[UIApplication sharedApplication] windows] lastObject] addSubview:popupView]
添加到了最顶部的窗口中,因此它将出现在所有东西的顶部,甚至包括键盘。 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];
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. 您不知道该窗口的真正所有者现在或将来的iOS版本会做什么。
Create your own window and set its windowLevel
high enough to be above the keyboard. 创建您自己的窗口,并将其windowLevel
设置得足够高以位于键盘上方。 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 . 您将在此问题的答案中找到许多有用的信息。
将高度和宽度约束添加到popupView是可以的,但是由于popupView是窗口的子视图,因此应该将居中约束添加到窗口,而不是popupView(并且您需要先将popupView作为子视图添加,然后再添加添加约束)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.