在Swift中何时为视图/图层设置边界和框架? [英] When is bounds and frame set for views/layers in Swift?

查看:74
本文介绍了在Swift中何时为视图/图层设置边界和框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIView 子类中有这样的内容:

I have something like this in a UIView subclass:

override var bounds : CGRect {
  didSet {
    somelayer.frame = bounds
  }
}

在相应的 somelayer 中,我有以下内容:

In the corresponding somelayer, I have this:

override var bounds: CGRect {
  didSet {
    someFunction()
  }
}

现在,如果使用 super.init(frame:someFrame)初始化视图,则不会调用第一个块。这似乎表明边界属性未与框架一起设置。

Now if the view is initialized using super.init(frame: someFrame), the first block is not called. This appears to indicate that the bounds property is not set alongside frame.

如果将第一个块更改为 override var frame ,则它将由 super.init(frame:someFrame),它将设置 somelayer 框架。有趣的是,发生这种情况时,将调用第二个块,建议同时设置边界 frame

If I change the first block to override var frame, then it will be called by super.init(frame: someFrame), and it will set the frame of somelayer. Interestingly, when that happens, the second block is called, suggesting that bounds and frame are set at the same time (which is what I'd expect, too).

我在这里错过了什么吗?

Am I missing anything here?

推荐答案

属性观察器


将首先初始化属性
时不调用willSet和didSet观察者。仅当在初始化上下文之外将属性的值设置为
时,才调用它们。

willSet and didSet observers are not called when a property is first initialized. They are only called when the property’s value is set outside of an initialization context.

也许是通过视图的方式

然后,当设置图层的框架时,边界也将通过内部方法进行更新。

Then later, when setting the frame of the layer, the bounds are also updated by the internal method.

这篇关于在Swift中何时为视图/图层设置边界和框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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