在其子类中从何处获取自定义UIView的帧大小 [英] Where to get frame size of custom UIView in its subclass
问题描述
当我使用storyboard添加自定义UIView时,是否计算了UIView的正确帧大小?
我无法在 UIView的 init(frame:CGRect)
或 init(编码器)中获得正确的帧大小aDecoder:NSCoder)
或 awakeFromNib()
。我在覆盖func layoutSubviews()
中获得了正确的大小,但这次视图未添加到视图控制器的视图中。
编辑:
我想在UIView子类中执行此操作,因为我添加了 CAGradientLayer
图层(与我的视图大小相同)到我的自定义 UIView
。必须有一个比在 UIViewController
s viewDidLayoutSubviews
方法中设置视图更好的方法。
有两个垂直约束(超级视图的尾随和前导空间),用于调整自定义视图的宽度。但是在上述方法中宽度为600,而不是屏幕宽度。
func viewWillAppear(_ animated:Bool)
在视图层次结构就位后,在viewController上调用几何体已设置。
它通常是安排几何相关的viewController代码的最佳位置,并且在转换动画运行之前调用。
然后调用 viewDidAppear
,在过渡动画完成后调用。
更新
因为你想直接在自定义视图中设置它(而不是在viewController中) )要覆盖的相应UIView函数是 layoutSubviews
。请务必在覆盖中调用 super.layoutSubviews()
。
覆盖func layoutSubviews(){
super.layoutSubviews()
//self.frame在这里是正确的
}
这将在viewController的 viewWillAppear
之后和 viewDidAppear
<之前调用/ p>
更新2:collectionView单元格
集合视图单元格从集合视图的布局中获取其帧数据。当一个单元格需要知道它的几何形状时,你可以覆盖 applyLayoutAttributes
func applyLayoutAttributes(_ layoutAttributes:UICollectionViewLayoutAttributes!)
默认layoutAttributes之一是frame属性。另请参阅下面有关如何直接从collectionView.layout中提取帧的注释。
When does the correct frame size of the UIView is calculated when I add my custom UIView by using storyboard?
I can't get correct frame size in UIView's init(frame: CGRect)
or init(coder aDecoder: NSCoder)
or awakeFromNib()
. I get the correct size in override func layoutSubviews()
but this time the view is not added to the view controller's view.
Edit:
I want to do this in the UIView subclass because I add a CAGradientLayer
layer (which has same size with my view) to my custom UIView
. There must be a better way than setting up the view in UIViewController
s viewDidLayoutSubviews
method.
There are two vertical constraints (trailing and leading space to superview) that adjust the width of my custom view. But the width is 600 in the mentioned methods, not the screen width.
func viewWillAppear(_ animated: Bool)
This is called on the viewController after the view hierarchy is in place and the geometry is set up.
It is usually the best place to arrange geometry-dependent viewController code, and is called just before transition animations are run.
It is followed by a call to viewDidAppear
which is called after transition animations have completed.
update
as you want to set this directly in a custom view (rather than in the viewController) the appropriate UIView function to override is layoutSubviews
. Be sure to call super.layoutSubviews()
in the override.
override func layoutSubviews() {
super.layoutSubviews()
//self.frame will be correct here
}
This will be called after the viewController's viewWillAppear
and before viewDidAppear
update 2: collectionView cells
Collection view cells get their frame data from the collectionview's layout. When a cell needs to know it's geometry, you can override applyLayoutAttributes
func applyLayoutAttributes(_ layoutAttributes: UICollectionViewLayoutAttributes!)
One of the default layoutAttributes is the frame property. See also my comment below on how to extract the frame directly from collectionView.layout.
这篇关于在其子类中从何处获取自定义UIView的帧大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!