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

旧时模样 提交于 2019-11-27 20:03:44
Ankit Khanna

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
    }
  }
}

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();
        });

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];
});

As mentioned in a previous answer, adding self.layoutIfNeeded() before changing the corner radius worked for me.

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()
        }
    }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!