Ios 在应用程序中添加约束

Ios 在应用程序中添加约束,ios,uiview,autolayout,nslayoutconstraint,Ios,Uiview,Autolayout,Nslayoutconstraint,我有一个自定义UIView,我将其加载到我的ViewController上 func loadBottomSheet(){ bottomSheet = Bundle.main.loadNibNamed("BottomSheetTest", owner: self, options: nil)![0] as! BottomSheetTest bottomSheet?.setUp(parentController: self) bottomShee

我有一个自定义UIView,我将其加载到我的ViewController上

 func loadBottomSheet(){

        bottomSheet = Bundle.main.loadNibNamed("BottomSheetTest", owner: self, options: nil)![0] as! BottomSheetTest
        bottomSheet?.setUp(parentController: self)
        bottomSheet?.tag = 9009164
        self.view.addSubview(bottomSheet!)
    }
自定义视图的代码如下所示:

 func setUp(parentController: UIViewController){

        self.parentController = parentController;

        self.translatesAutoresizingMaskIntoConstraints = false

        self.leftAnchor.constraint(equalTo: parentController.view.leftAnchor).isActive = true
        self.rightAnchor.constraint(equalTo: parentController.view.rightAnchor).isActive = true
        self.heightAnchor.constraint(equalToConstant: CGFloat(400)).isActive = true
        self.bottomAnchor.constraint(equalTo: parentController.view.bottomAnchor).isActive = true


    }
我正在尝试将它添加到我的视图控制器。以下代码导致应用程序崩溃,并出现一般错误:libc++abi.dylib:以NSException类型的未捕获异常终止


现在,我已经从自定义视图中删除了所有内容。你在这里看到的就是它的全部,没有更多的UI元素,没有IBOutlets,就是这样。我束手无策。

您正在为不在视图层次结构中的视图添加约束,因此首先需要将所需视图添加为子视图,然后可以设置约束而不会在过程中崩溃

您需要移动此行

self.view.addSubview(bottomSheet!)
在这个上面

bottomSheet?.setUp(parentController: self)