如何将 swift NSLayoutConstraint activateConstraints 翻译成 objective c?

How do I translate swift NSLayoutConstraint activateConstraints to objective c?

我在 swift 中成功创建了一个项目,我在 viewController 中按下按钮将其他 viewControllersxib 文件加载到容器中。只是给你一个想法:

它工作得很好,但我很难将它翻译成 objective-c,而这正是真正需要代码的地方。这是按下按钮时 swift 代码的样子:

let sub3 = UINib(nibName: "thirdXib", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView

containerOutlet.addSubview(sub3)

NSLayoutConstraint.activate([
    sub3.leadingAnchor.constraint(equalTo: containerOutlet.leadingAnchor),
    sub3.trailingAnchor.constraint(equalTo: containerOutlet.trailingAnchor),
    sub3.topAnchor.constraint(equalTo: containerOutlet.topAnchor),
    sub3.bottomAnchor.constraint(equalTo: containerOutlet.bottomAnchor)
        ])

现在,我希望所有代码都在 objective-c 中。在 objective-c 我已经完成了第一部分,我认为这是正确的:

UINib *nib = [UINib nibWithNibName:@"SecondView" bundle:nil];
[nib instantiateWithOwner:self options:nil];

[containerOutlet addSubview:((UIView*)nib)];

但是我对最后一部分有疑问,NSLayoutConstraint activateConstraints

[NSLayoutConstraint activateConstraints:@[
        [nib.lead],
        [nib.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        ]
 ];

编译器说 property lead not found on object of type UIBib 和下面的代码相同。我已经尝试了这些的变体,但没有做对。如何在我的 xib 上设置尾随、前导、底部和顶部约束?我需要添加 #import SecondView.xib" in the.h` 文件吗?顺便问一下?

你可以试试

UINib *nib = [UINib nibWithNibName:@"SecondView" bundle:nil]; 
UIView *sub3 = [nib instantiateWithOwner:self options:nil][0]; 
[containerOutlet addSubview:sub3];


[NSLayoutConstraint activateConstraints:@[
   [sub3.leadingAnchor constraintEqualToAnchor:scontainerOutlet.leadingAnchor],
   [sub3.trailingAnchor constraintEqualToAnchor:containerOutlet.trailingAnchor],
   [sub3.topAnchor constraintEqualToAnchor:containerOutlet.topAnchor],
   [sub3.bottomAnchor constraintEqualToAnchor:containerOutlet.bottomAnchor]
]];