gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:51:58 25 4
gpt4 key购买 nike

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

enter image description here

它工作得很好,但我很难将它翻译成 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 上设置尾随、前导、底部和顶部约束?我需要在 .h` 文件中添加 #import SecondView.xib"吗?顺便说一句?






最佳答案





你可以试试



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]
]];

关于objective-c - 如何将 swift NSLayoutConstraint activateConstraints 翻译成 objective-c ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58269080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com