ios10: viewDidLoad frame width/height not initialized correctly

前端 未结 7 952
闹比i
闹比i 2020-12-07 20:23

Since upgrading to XCode8 GM and ios10, all of my views created via Interface Builder are not being initialized correctly until much much later than expected. This means in

7条回答
  •  执笔经年
    2020-12-07 20:59

    The most common issues you describe are appearing in iOS 10 only and can be solved by adding this line (if necessary):

    self.view.layoutIfNeeded()
    

    just above the code, that is responsible for changing constraint, layer.cornerRadius etc.

    OR

    place your code related to frames / layers into viewDidLayoutSubviews() method:

    override func viewDidLayoutSubviews() {
    
        super.viewDidLayoutSubviews()
        view.layer.cornerRadius = self.myView.frame.size.width/2
        view.clipsToBounds = true
    
        ... etc
    }
    

提交回复
热议问题