何时在iOS10 SDK中设置视框大小? [英] When is view frame size set in iOS10 SDK?
问题描述
多年来,在Swift和ObjC中,我一直使用这种技术制作圆形视图:
For several years in Swift and ObjC I've used this technique to make a circular view:
view.layer.cornerRadius = view.frame.size.width / 2
view.clipsToBounds = true
当情节提要中的UILayoutConstraints固定为宽度/高度时,将此代码放在viewDidLoad或viewWillAppear中就没有问题.内置iOS9.3SDK,可在iOS10等系统中正常运行.
When UILayoutConstraints in the Storyboard are fixed width / height there has been no problem putting this code in viewDidLoad, or in viewWillAppear. Built in iOS9.3SDK it runs fine in iOS10 etc.
iOS10SDK在Storyboard中显示的帧大小与固定大小完全不同,甚至在viewWillAppear和viewDidLayoutSubviews等中也是如此.我的选择是:
iOS10SDK shows framesize completely different to the fixed size in the Storyboard, even up to viewWillAppear, and in viewDidLayoutSubviews etc. My options are:
1)在viewDidAppear中执行此操作(糟糕的解决方案) 2)对CornerRadius进行硬编码(工作正常,但效果糟糕)
1) do this in viewDidAppear (awful solution) 2) hardcode the cornerRadius (works fine, but awful)
这看起来像是个错误(因为在控制台中至少没有警告的情况下,不得更改情节提要中设置的固定宽度/高度).是吗,或者此代码现在有其他位置? (随附测试代码的屏幕截图)
This looks like a bug (as a fixed width/height set in Storyboard should never be changed without at least a warning in the console). Is it, or is there a different place for this code now? (screenshot of test code attached)
推荐答案
在iOS 10中,我是在6月开始使用Xcode 8(当时是Beta版)后立即遇到此问题的.需要此解决方案:
In iOS 10, I met this issue right after I started to use Xcode 8 in june, when it was beta. This solution is needed:
在修改图层之前放置layoutIfNeeded()
:
self.view.layoutIfNeeded()
view.layer.cornerRadius = view.frame.size.width / 2
view.clipsToBounds = true
OR
将转角半径的代码放入viewDidLayoutSubviews()
方法:
place the code for corner radius to viewDidLayoutSubviews()
method:
override func viewDidLayoutSubviews() {
view.layer.cornerRadius = view.frame.size.width / 2
view.clipsToBounds = true
}
这篇关于何时在iOS10 SDK中设置视框大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!