Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么在willMove(toSuperView)方法中未根据superview正确设置自动布局约束_Ios_Swift_Uikit - Fatal编程技术网

Ios 为什么在willMove(toSuperView)方法中未根据superview正确设置自动布局约束

Ios 为什么在willMove(toSuperView)方法中未根据superview正确设置自动布局约束,ios,swift,uikit,Ios,Swift,Uikit,我在设置自定义uiview的约束时遇到了一个问题,自定义uiview是从nib加载并作为子视图添加到containerview的。我试图在willMove(toSuperView)方法中添加约束,但它没有按预期工作。在哪里设置自定义ui视图的约束最合适 这是代码 extension ErrorView { override func willMove(toSuperview newSuperview: UIView?) { super.willMove(toSuperv

我在设置自定义uiview的约束时遇到了一个问题,自定义uiview是从nib加载并作为子视图添加到containerview的。我试图在
willMove(toSuperView)
方法中添加约束,但它没有按预期工作。在哪里设置自定义
ui视图的约束最合适

这是代码

extension ErrorView {

    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        guard let newSuperview = newSuperview else { return }
        translatesAutoresizingMaskIntoConstraints = false
        newSuperview.setConstraintHeight(to: self)
        SetConstraintWidth(to: newSuperview)
    }
}

setConstraintWidth
setconstrainthight
方法名称是占位符。您可以认为它们是设置约束的方法。

根据我所知,覆盖layoutsubviews()将完成这项工作

override func layoutSubviews() {
    super.layoutSubviews()
    //funky stuff here
}
对于类似的问题,这里有一个很好的答案:

来自苹果的文档():

通知视图其超级视图将更改为指定的超级视图

因此,这是在视图添加到新视图之前发生的


重写
didAddSubview()
可能是添加约束的更好地方。

是否使用代码将子类
UIView
添加到容器视图?