Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView frames wrong in viewDidLayoutSubviews after upgrade to Xcode 8

Tags:

xcode

xib

frame

In each of my xibs it asked me what default device do I want to use. I chose iPhone 6 and updated the frames to the suggested ones. Now, any view that was affected in the viewWill or viewDidLayoutSubviews is messed up. They all have frames of CGRect(0, 0, 1000, 1000).

Here's my code:

self.headImage.layer.cornerRadius = ceilf(CGRectGetWidth(self.headImage.frame)/2);
self.headImage.layer.masksToBounds = YES;

Wondering what's up with this and whether there is a solution. Thanks

like image 799
PiscesZjc Avatar asked Sep 19 '16 01:09

PiscesZjc


2 Answers

I resolved my issue by calling

myOwnView.layoutIfNeeded() 

before getting the myOwnView.frame

like image 97
India Avatar answered Jun 03 '23 16:06

India


If even viewDidLayoutSubviews gives you the wrong frame - calling layoutIfNeeded on layoutSubviews can do the trick!

override func layoutSubviews() {
    super.layoutSubviews()

    yourView.layoutIfNeeded()
    setGradientForYourView()

}

like image 27
Vitya Shurapov Avatar answered Jun 03 '23 16:06

Vitya Shurapov