Reputation: 1029
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
Upvotes: 33
Views: 11052
Reputation: 26
Swift 3.2 Solution
In case anyone still having this problem, I solved the problem by rounding the cells (or any of its subviews) in viewDidLayoutSubviews.
override func viewDidLayoutSubviews() {
if (tableView.visibleCells.count > 0) {
for cell in tableView.visibleCells {
let customCell = cell as! CustomCell
// Use any rounding method you use
customCell.exampleSubview.roundCorners(corners: .allCorners, radius: 6)
customCell.exampleSubview.layoutIfNeeded()
}
}
}
Upvotes: 0
Reputation: 1112
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
}
}
}
Upvotes: 1
Reputation: 1448
swift 3
I will try this code to define corner radius of UIImageview
imgProfile.layer.cornerRadius = imgProfile.frame.size.width / 2
imgProfile.clipsToBounds = true
Upvotes: 1
Reputation: 11
As mentioned in a previous answer, adding self.layoutIfNeeded()
before changing the corner radius worked for me.
Upvotes: 0
Reputation: 497
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
Upvotes: 3
Reputation: 1029
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.
Upvotes: 35
Reputation: 6197
I had to run it on the main thread for it to work.
dispatch_async(dispatch_get_main_queue(), ^{
[imageView.layer setCornerRadius:4];
[imageView.layer setMasksToBounds:YES];
});
Upvotes: 0
Reputation: 9649
For me what it worked was first call layoutIfNeeded and later on set cornerRadius
Upvotes: 2
Reputation: 49
In WWDC 2016 | What's New in Auto Layout, Apple recommend a way of autolayout named
Incrementally Adopting Autolayout
It means if the layout is easy, you can use autoresizing. And it will translate to constraints at run time.
So one of the solutions is use autoresizing, and you can get right frame about the view in anytime. But the premise is the autoresizing can satisfy your layout
Upvotes: -1
Reputation: 2149
I had the same issue, iOS 10 only. I found that ViewDidLayoutSubviews or ViewDidLoad (possibly others too but I didn't check) only worked if I wrapped the layout code in the dispatch queue as YPK suggested.
I am using Xamarin so the syntax is a bit different:
DispatchQueue.MainQueue.DispatchAsync(() =>
{
SetYourViewProperties();
});
Upvotes: 0
Reputation: 1851
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
Upvotes: 4
Reputation: 12594
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)
Upvotes: 8