提前抱歉这个可能很愚蠢的问题 - 我是初学者
我正在 viewDidLoad 中生成一个新的 UIView,提供一些约束以将其锚定在主视图上。当谈到理解新视图的大小时,我总是得到零。
这是我的代码的简化版本,它不起作用:
override func viewDidLoad() {
super.viewDidLoad()
let myView = UIView(frame: .zero)
myView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(myView)
myView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
myView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -200).isActive = true
myView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
myView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
print(myView.bounds.width)
}
宽度(还有高度)返回 0。这在 .bounds 和 .frame 中都会发生。你有什么提示吗?谢谢!