gpt4 book ai didi

ios - 使用自动布局使 CustomView 适合 SuperView

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:50 24 4
gpt4 key购买 nike

我正在尝试创建一个基于此 tutorial 翻转的卡片 View .它创建一个 UIView 并添加卡片 View 。

为此,我创建了一个自定义 UIView(使用 Xib),到目前为止它运行良好。我在 Storyboard中为调用 addSubview 的 View 添加了正确的约束。到目前为止,这是有效的,但是当我添加自定义 UIView 时,它指的是它在 xib 中的大小,而不是 super View 的大小。

如何添加必要的自动布局约束以使 subview 适合其父 View ?

谢谢!

附言我已经用 Objective-C 编写了它,但它对我来说并不重要,无论答案是用 swift 还是 Objective-C。

最佳答案

不确定,有什么问题。我通过以下方式做了同样的事情:

1) ProductItemView,UIView 的子类,并创建了具有 UIView 对象的 ProductItemView.xib。

2) 在 .xib 中将 File 的所有者类设置为 ProductItemView,以便可以加载 .xib 的 UIView 对象和其他 subview 并与 IBOutlet 链接。(见图)

enter image description here

3)在初始化方法(initWithFrame:在我的例子中)中,方法放在代码下面

    NSArray *nibViewArray = [[NSBundle mainBundle] loadNibNamed:@"ProductItemView" owner:self options:nil];

if (nibViewArray.count) {
UIView *nibView = [nibViewArray objectAtIndex:0];
[self addSubview:nibView];

nibView.translatesAutoresizingMaskIntoConstraints = NO;

NSDictionary *viewDict = NSDictionaryOfVariableBindings(nibView);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[nibView]-0-|" options:0 metrics:nil views:viewDict]];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[nibView]-0-|" options:0 metrics:nil views:viewDict]];

}

最后创建 ProdutItemView 的对象并使用框架或设置约束并将其添加到 super View 。如果您正在设置约束,那么不要忘记将 translatesAutoresizingMaskIntoConstraints 属性设置为 NO。希望对您有所帮助。

关于ios - 使用自动布局使 CustomView 适合 SuperView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003085/

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