gpt4 book ai didi

Swift 4 - 如何在以编程方式使用约束后获取 UIView 的边界

转载 作者:搜寻专家 更新时间:2023-10-31 19:27:30 26 4
gpt4 key购买 nike

关于这个问题,我真的需要你的帮助。我正在以编程方式创建一个 UIView 对象,而没有在开始时设置边界。创建后,我向该 View 添加约束,以适应屏幕中所需的位置。

但问题是,我想在代码的以下部分使用 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 属性时,它会将其原始大小返回给我。

如何使用相关约束设置获得屏幕上显示的边界?

非常感谢你们。

最佳答案

您可以在

中访问它
 override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if once {
once = false
// process here
print(previewView.bounds)
}
}

但首先将 View 声明为实例变量,使once = false in view

var previewView:UIView! 
var once= true

看这里enter image description here

关于Swift 4 - 如何在以编程方式使用约束后获取 UIView 的边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49182778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com