简体   繁体   English

如何获得视图的底部边缘?

[英]How to get bottom edge of the view?

I looked at the following post in order to understand how to get the y-coordinate of the bottom edge of a view: How get bottom y coordinate 我看了以下文章,以了解如何获取视图底部边缘的y坐标如何获取底部y坐标

It says I can do this: exampleView.frame.maxY 它说我可以做到这一点: exampleView.frame.maxY

However, when I do this I get 0. Here is my example: 但是,当我这样做时,我得到0。这是我的示例:

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let thankYouMessage = UILabel()
        self.view.addSubview(thankYouMessage)

        thankYouMessage.text = "Thank You for Installing the Keyboard"
        thankYouMessage.textAlignment = .Center

        thankYouMessage.snp_makeConstraints { (make) -> Void in
            make.right.left.top.equalTo(self.view)
            make.height.equalTo(self.view.frame.height * 0.2)
        }

        print(thankYouMessage.frame.maxY)

        let enableMessage = UILabel()
        self.view.addSubview(enableMessage)
        enableMessage.text = "Enable the keyboard here:"
        enableMessage.textAlignment = .Center
        enableMessage.backgroundColor = UIColor.blackColor()

        enableMessage.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.view)
            make.top.equalTo(self.view).offset(50)
            make.width.equalTo(self.view.frame.width * 0.3)
            make.height.equalTo(self.view.frame.height * 0.1)
        }




    }

The print(thankYouMessage.frame.maxY) returns 0. print(thankYouMessage.frame.maxY)返回0。

I am not sure why this is. 我不确定为什么会这样。 Is there some sort of initialization process I need to do to the first view? 我需要对第一个视图执行某种初始化过程吗?

The view's frame isn't updated based on its constraints and/or its intrinsic content size until the layout pass has run. 在布局遍历运行之前,不会根据其约束和/或其固有内容大小来更新视图的框架。 The earliest you can get the view's on-screen frame in the view controller is in the viewDidLayoutSubviews method. 最早可以在视图控制器中获取视图的屏幕框架的方法是viewDidLayoutSubviews方法。

You could call layoutIfNeeded or sizeToFit on the view before asking for its frame, but depending on the situation those could still end up giving the wrong answer if called in viewDidLoad . 您可以在请求框架之前在视图上调用layoutIfNeededsizeToFit ,但是根据情况,如果在viewDidLoad调用,仍然可能给出错误的答案。

It happened because you created your label with zero frame and your view is not added to hierarchy in the window in viewDidLoad method. 发生这种情况的原因是,您使用零帧创建了标签,并且未在viewDidLoad方法的窗口中将视图添加到层​​次结构中。 The documentation say's that in viewDidLoad view has been loaded only into memory. 文档说的是,在viewDidLoad视图中仅被加载到内存中。 And in viewWillAppear it added to hierarchy in the window. 并在viewWillAppear中将其添加到窗口的层次结构中。 Put your print to viewWillAppear for check it or created UILabel() with frame 将您的打印内容放在viewWillAppear上进行检查,或使用框架创建UILabel()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在自动布局ios中将视图的底部边缘和顶部边缘与另一个视图冲突? - How to contraint the bottom edge and top edge of a view to another view in auto layout ios? 如何在集合视图单元的内部底部边缘添加阴影? 迅捷3 - How to add a shadow to the inside bottom edge of a collection view cell? swift 3 NSLayoutConstraint可以将视图固定到superview的下边缘 - NSLayoutConstraint that would pin a view to the bottom edge of a superview 在View(iOS)的底部边缘触摸不注册 - Touch not registering at bottom edge of View (iOS) iOS:使用固定的长宽比将视图约束到后缘或下边缘 - iOS: Constraining View with fixed aspect ratio to trailing edge OR bottom edge SwiftUI:如何将视图作为参数传递给另一个视图以获取底部工作表? - SwiftUI: How to pass a View as a parameter into another View to get Bottom Sheet? SwiftUI - 如何将底边与兄弟中心对齐 - SwiftUI - How to align bottom edge to center of sibling 是否无法约束标签的基线以匹配另一个视图的底边? - Is there no way to constrain the baseline of a label to match the bottom edge of another view? 从边缘滑动后如何从底部滑动按钮? - How to slide a button from the bottom after sliding from the edge? 如何在 SwiftUI 中淡化 SF 符号的顶部或底部边缘? - How Do I Fade the Top or Bottom Edge of a SF Symbol in SwiftUI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM