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
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.
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)
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
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
For me what it worked was first call layoutIfNeeded and later on set cornerRadius
swift 3
I will try this code to define corner radius of UIImageview
imgProfile.layer.cornerRadius = imgProfile.frame.size.width / 2
imgProfile.clipsToBounds = true
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
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With