如果在UIView中使用代码实现AutoLayout,我得到的是UIView框架宽度的大小?
mainView.backgroundColor = UIColor.green
self.view.addSubview(mainView)
mainView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0.0).isActive = true
mainView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0.0).isActive = true
mainView.widthAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 0.5).isActive = true
mainView.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 0.5).isActive = true
print(mainView.frame.width)//0.0
答案 0 :(得分:0)
我通过在视图中绘制布局后获取视图的大小来解决它
override func viewDidAppear(_ animated: Bool) {
print(mainView.frame.width)
}