Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cornerRadius stopped working in Swift 2.3 / iOS 10 / Xcode 8

I have a cornerRadius set on a UIView and a UIImageView inside the same UIView. I am calculating the corner radius with RockProfileView.frame.size.height / 2 but the UIView stopped showing in iOS 10.

After further checking i found the value of RockProfileView.frame.size.height / 2 is coming out to be 1000.0 while the width and height constraint is set to 64.0

When I hardcoded the RockProfileView.layer.cornerRadius = 32 to 64/2 it works just fine.

What could be the issue ?

Full code:

    RockProfileView.layer.cornerRadius = RockProfileView.frame.size.height / 2
    RockProfileView.clipsToBounds = true
    RockProgressView.layer.masksToBounds = true
like image 864
Ankit Khanna Avatar asked Sep 15 '16 05:09

Ankit Khanna


7 Answers

As answered by Rob, I've moved the code from viewDidLoad to viewDidAppear and the problem is fixed.

OR Adding self.view.layoutIfNeeded() before your code in viewDidLoad also solves the issue.

In case of UITableViewCell, Inside awakeFromNib add [self layoutIfNeeded]; before updating the corner radius should solve all the issues.

like image 56
Ankit Khanna Avatar answered Sep 26 '22 23:09

Ankit Khanna


I were done this code in awakeFromNib, but after upgrading to ios 10+xcode 8, it stopped working.

Then i moved this code to layoutSubViews method. Then it worked.

Hoping, this will be be useful to you.

If you want to still do this in awakefromnib, then do this after putting some delay(by using dispatch_after or NSOperatinQueue or performSelectorWithDelay)

like image 44
Mehul Thakkar Avatar answered Sep 23 '22 23:09

Mehul Thakkar


If it is inside your one of ViewControllers, instead of moving all layer operations to viewDidLayer, move that code inside DispatchQueue. This worked for me:

DispatchQueue.main.async {
            let layer = self.signUpBtn.layer
            layer.borderColor = UIColor.white.cgColor
            layer.borderWidth = 2 / UIScreen.main.scale
        }

Hope this helps for you

like image 28
YPK Avatar answered Sep 23 '22 23:09

YPK


New one thing into XCode8 we can't directly set the cornerRadius of the layer.

When you want to apply cornerRadius of UIView need to add one line of code before applying cornerRadius.

yourButton.layoutIfNeeded()

Example into Objective C.

[yourButton layoutIfNeeded];
yourButton.layer.cornerRadius = yourButton.frame.size.height/2;
[[yourButton layer] setBorderWidth:2.0f];
Example into Swift3

self.layoutIfNeeded()
yourButton.layer.cornerRadius = self.frame.height / 2.0
yourButton.layer.borderWidth = 2.0
like image 25
Sunil aruru Avatar answered Sep 24 '22 23:09

Sunil aruru


For me what it worked was first call layoutIfNeeded and later on set cornerRadius

like image 27
Javier Calatrava Llavería Avatar answered Sep 26 '22 23:09

Javier Calatrava Llavería


swift 3

I will try this code to define corner radius of UIImageview

 imgProfile.layer.cornerRadius = imgProfile.frame.size.width / 2
            imgProfile.clipsToBounds = true
like image 39
Amul4608 Avatar answered Sep 26 '22 23:09

Amul4608


Marked with @IBInspectable in swift (or IBInspectable in Objective-C), they are easily editable in Interface Builder’s attributes inspector panel.
You can directly set cornerRadius in attributes inspector

extension UIView {

  @IBInspectable var cornerRadius: CGFloat {

   get{
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue
        layer.masksToBounds = newValue > 0
    }
  }
}
like image 20
Amit Jagesha シ Avatar answered Sep 26 '22 23:09

Amit Jagesha シ