Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clipsToBounds causes UIImage to not display in iOS10 & XCode 8

I switched my project over to new beta versions of iOS 10 and XCode 8. In all three areas of my app where I use:

imageView.layer.cornerRadius = imageView.frame.size.width/2
imageView.clipsToBounds = true

The associated images are not displaying at all. I have attempted cleaning the project as well as the build folder, restarting the device, trying on various simulators, re-adding the imageView, programmatically setting the associated UIImage instead of choosing one from the assets.

Removing the clipsToBounds line shows the rectangular image regardless of whether masksToBounds is true or false. How can I make a circular image in XCode8 / iOS10 ?

Edit: The project is Swift 2.x and not yet updated to Swift 3.0 syntax.

like image 402
cloudcal Avatar asked Aug 25 '16 04:08

cloudcal


3 Answers

I had the same problem and calling layoutIfNeeded before all the rounded corner / clipsToBounds stuff fixed the issue. iOS 10 GM with xcode 8 GM causes views to disappear due to roundedCorners & clipsToBounds

like image 120
sudoExclaimationExclaimation Avatar answered Oct 21 '22 05:10

sudoExclaimationExclaimation


This problem also happened to me.

I moved these code imageView.layer.cornerRadius = imageView.frame.size.width/2 from - (void)awakeFromNib to - (void)drawRect:(CGRect)rect and this problem went away.

My imageView is sized by autolayout. I think that height and width are not decided when awaking from nib on iOS 10.

like image 31
VictorJi Avatar answered Oct 21 '22 05:10

VictorJi


This sounds like it could be due to a new bug since iOS 10 and Xcode 8 where views are initialised at size 1000x1000 and when trying to set corner radius to half your frame, it is setting it to 500 instead. This is documented further in this question here: Since Xcode 8 and iOS10, views are not sized properly on viewDidLayoutSubviews. My reason for thinking this is the fix to the 1000x1000 issue it to call layout subviews before doing anything that requires sizes for something that has been constructed on the interface builder.

like image 31
Trever123 Avatar answered Oct 21 '22 06:10

Trever123