简体   繁体   English

当视图位于视图层次结构的顶部时,使用自动布局以编程方式在屏幕上居中放置UIView

[英]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.

相关问题 使用自动布局以编程方式创建视图层次结构 - Create view hierarchy programmatically with Auto Layout 将以编程方式创建的UIView中心与自动布局视图的中心对齐 - Align a programmatically created UIView's center with an auto-layout view's center 自动布局视图层次结构 - Auto Layout View Hierarchy UIView层次结构中的顶级视图 - Top level view in a UIView hierarchy iOS - 以编程方式使用自动布局将UIView置于超视图的中心 - iOS - Position UIView in center of superview using Auto Layout programmatically 在Swift中,以编程方式创建UIView并向其中添加控件并使用自动布局会导致控件出现在视图的父级上 - In Swift, programmatically creating UIView and adding controls to it and using auto layout, causes the controls to appear on the view's parent 使用自动版式将视图放置在表格视图的顶部 - Positioning a view on top of a tableview using Auto Layout 将UIView插入视图控制器视图的层次结构时,它不位于集合视图单元格的顶部 - A UIView isn't placed right on top of a collection view cell when inserted to the view controller view's hierarchy 使用XIB和自动布局来操纵视图层次结构 - Manipulate view hierarchy using XIBs and auto-layout 使用自动布局隐藏UIView时以编程方式添加约束 - Add constraints programmatically when hiding a UIView using Auto Layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM