Swift 4 - 如何在以编程方式使用约束后获取 UIView 的边界
Swift 4 - How to get bounds of a UIView after Using Constraints Programatically
关于这个问题我真的需要你的帮助。我正在以编程方式创建一个 UIView 对象,而没有在开始时设置边界。创建后,我向该视图添加约束,以适应屏幕中所需的位置。
但问题是,我想在代码的以下部分使用 UIView 的边界 属性,但不幸的是,在设置约束后我无法获得屏幕上显示的边界。
下面我有示例代码:
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 属性 时,它 returns 我的原始大小为 0。
如何使用相关约束设置获得屏幕上显示的边界?
非常感谢你们。
您可以在
中访问它
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if once {
once = false
// process here
print(previewView.bounds)
}
}
但首先将视图声明为实例变量,在视图
中创建once = false
var previewView:UIView!
var once= true
看这里
关于这个问题我真的需要你的帮助。我正在以编程方式创建一个 UIView 对象,而没有在开始时设置边界。创建后,我向该视图添加约束,以适应屏幕中所需的位置。
但问题是,我想在代码的以下部分使用 UIView 的边界 属性,但不幸的是,在设置约束后我无法获得屏幕上显示的边界。
下面我有示例代码:
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 属性 时,它 returns 我的原始大小为 0。
如何使用相关约束设置获得屏幕上显示的边界?
非常感谢你们。
您可以在
中访问它 override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if once {
once = false
// process here
print(previewView.bounds)
}
}
但首先将视图声明为实例变量,在视图
中创建once = false
var previewView:UIView!
var once= true
看这里