[英]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
. 您可以在请求框架之前在视图上调用
layoutIfNeeded
或sizeToFit
,但是根据情况,如果在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.