cornerRadius 在 Swift 2.3/iOS 10/Xcode 8 中停止工作 [英] cornerRadius stopped working in Swift 2.3 / iOS 10 / Xcode 8

查看:29
本文介绍了cornerRadius 在 Swift 2.3/iOS 10/Xcode 8 中停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIView 上设置了 cornerRadius,并在同一个 UIView 内设置了 UIImageView.我正在使用 RockProfileView.frame.size.height/2 计算角半径,但 UIView 在 iOS 10 中停止显示.

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.

进一步检查后,我发现 RockProfileView.frame.size.height/2 的值是 1000.0,而宽度和高度约束设置为 64.0

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

当我将 RockProfileView.layer.cornerRadius = 32 硬编码为 64/2 时,它工作得很好.

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

可能是什么问题?

完整代码:

    RockProfileView.layer.cornerRadius = RockProfileView.frame.size.height / 2
    RockProfileView.clipsToBounds = true
    RockProgressView.layer.masksToBounds = true

推荐答案

正如 Rob 所回答的,我已经将代码从 viewDidLoad 移动到 viewDidAppear 并且问题是固定.

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

或在 viewDidLoad 中的代码之前添加 self.view.layoutIfNeeded() 也可以解决这个问题.

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

在 UITableViewCell 的情况下,在 awakeFromNib 里面添加 [self layoutIfNeeded]; 之前更新圆角半径应该可以解决所有问题.

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

这篇关于cornerRadius 在 Swift 2.3/iOS 10/Xcode 8 中停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆