【发布时间】:2018-03-08 21:29:14
【问题描述】:
在这个问题上我真的需要你的帮助。我正在以编程方式创建 UIView 对象,而没有在开始时设置边界。创建后,我向该视图添加约束以适应屏幕中所需的位置。
但问题是,我想在代码的以下部分中使用 UIView 的 bounds 属性,但不幸的是,在屏幕上显示的约束设置后,我无法获得边界。
下面我有示例代码:
let previewView = UIView()
NSLayoutConstraint(item: previewView, attribute: .top, relatedBy: .equal, toItem: upperView, attribute: .bottom, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: previewView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: 0.0).isActive = true
previewView.widthAnchor.constraint(equalToConstant: self.view.frame.width).isActive = true
previewView.translatesAutoresizingMaskIntoConstraints = false
print(previewView.bounds)
// (0.0, 0.0, 0.0, 0.0) ->>>> How can I get the bounds as it shows on the screen instead of 0,0,0,0
此后,当我尝试使用 previewView.bounds 属性时,它返回我原来的大小为 0。
如何通过相关的约束设置获得屏幕上显示的边界?
非常感谢你们。
【问题讨论】:
-
调用
preview.superview.layoutIfNeeded()后打印边界。
标签: swift autolayout constraints bounds