如何获得帧大小? - AutoLayout

时间:2018-01-11 13:41:55

标签: swift3 autolayout

如果在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

1 个答案:

答案 0 :(得分:0)

我通过在视图中绘制布局后获取视图的大小来解决它

override func viewDidAppear(_ animated: Bool) {
    print(mainView.frame.width)
}