如何获取已通过 snapkit 设置的 UIView 的框架

How to get the frame of a UIView that has been setup through snapkit

首先,我真的要感谢构建 snapkit 的人。它确实使为 UIViews 设置约束变得非常容易。

但现在,我有一个简单的问题:如何访问我使用此库设置的视图的框架 属性?

例如:

self.view.addSubview(contributePosterView)
self.contributePosterView.snp.makeConstraints { (make) in
    make.left.equalTo(self.view.snp.left)
    make.width.equalTo(self.view.bounds.width)
    make.top.equalTo(self.table.snp.bottom)
    make.bottom.equalTo(self.view.snp.bottom)
}

如何访问我命名为 contributePosterView 的视图的框架 属性?

这对我来说很重要,尤其是当我必须使用上述滚动视图的 layoutSubviews 属性 在 UIScrollview 中设置它们时。

我尽可能地检查了 snapkit 文档,但仍然没有找到答案。

我该怎么办?任何帮助将不胜感激。

"SnapKit" 提供了添加约束的方法,使用许多人认为比默认 NSLayoutContraint 方法更容易的语法。但是,它不会对视图执行任何操作以致无法获得结果帧大小。

问题是您可能在 viewDidLoad() 中进行 "snap" 调用,然后立即尝试获取帧。那时,所发生的只是添加了约束,但自动布局还没有完成它的工作。

您想覆盖 viewDidLayoutSubviews(),此时您可以获得有效的帧大小:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // now you can get the resulting frame
    let f = self.contributePosterView.frame

   // do what you want with the frame
}