• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 以编程方式自动布局不起作用

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:52:41 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想深入了解自动布局,所以我想以编程方式进行以便正确理解它。我知道如何通过 Storyboard很好地使用它。现在这就是我正在做的事情(在 viewDidAppear:)

-(void)scenario2{

    PGView *viewA = [[PGView alloc]initWithFrame:CGRectMake(100, 100, 100, 100) andBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:viewA];

    PGView *viewB = [[PGView alloc]initWithFrame:CGRectMake(200, 300, 100, 100) andBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:viewB];

    viewA.translatesAutoresizingMaskIntoConstraints = NO;
    viewB.translatesAutoresizingMaskIntoConstraints = NO;

    NSLayoutConstraint *constraint;

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"V:[viewB(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewB)]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"H:[viewB(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewB)]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"V:[viewA(==200)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewA)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"H:[viewA(==200)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewA)]];

    //problem statement
    constraint = [NSLayoutConstraint constraintWithItem:viewA attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:viewB attribute:NSLayoutAttributeWidth multiplier: 3.0f constant: 0.0f];
    [self.view addConstraint:constraint];

    NSLog(@"%@",viewA);
    NSLog(@"%@",viewB);

}
  1. 所以这里我初始化了两个不同颜色的 View 。

  2. 我知道我需要设置属性。在应用任何新约束之前,translatesAutoresizingMaskIntoConstraintsNO。所以我做到了。

  3. 现在我正在指定两个 View 的高度和宽度。所以,两个 View 的 x_position 和 y_position 都是 0。我运行代码并得到预期的结果。两个 View 在点 (0,0) 处都有指定的高度和宽度。

问题

  1. 现在我在写“问题陈述”根据viewA的宽度调整viewB的宽度时,它不起作用。

  2. 另外,当我打印 View viewA 和 viewB 时,它会将帧打印为 (0,0,0,0)。

谁能解释一下这种行为?


编辑:这是我所做的一些修改。我已经使用“hasAmbiguousLayout”检查了两个 View 的歧义,现在它工作正常。现在如果我想将 viewB 的宽度调整为三次,下一步应该是什么viewA 的宽度?

-(void)scenario2{

PGView *viewA = [PGView new];
viewA.backgroundColor = [UIColor yellowColor];
[self.view addSubview:viewA];

PGView *viewB = [PGView new];
viewB.backgroundColor = [UIColor blackColor];
[self.view addSubview:viewB];

viewA.translatesAutoresizingMaskIntoConstraints = NO;
viewB.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint *constraint;

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"V:|-[viewB(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewB)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"H:|-[viewB(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewB)]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"V:|-[viewA(==200)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewA)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"H:|-[viewA(==200)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewA)]];


NSLog(@"%d",viewA.hasAmbiguousLayout);
NSLog(@"%d",viewB.hasAmbiguousLayout);

NSLog(@"%@",viewA);
NSLog(@"%@",viewB);

}



Best Answer-推荐答案


问题在于您已将 A 和 B 的宽度限制分别明确设置为 200 和 100。因此,当与其他约束结合时,指定一个宽度应为另一个宽度三倍的附加约束是不可满足的。

此外,约束是不明确的,因为您已经定义了宽度和高度约束,但还没有定义 where 两个框架应该在哪里。是的,您已经为这两个 View 定义了 frame,但是在应用约束时会忽略它。您可能根本不应该定义 frame 值,因为它具有误导性和混淆性。但是您应该设置,例如,前导和顶部约束,或者在 VFL 中指定这些,或者添加 centerXcenterY 约束。您只需要一些约束来指定 View 的放置位置,并且有很多方法可以指定它。

最后你没有看到合理的 frame 值的原因是你已经定义了约束,但是它们还没有被应用。如果需要,您可以在 viewDidLayoutSubviews 中检查它们。


你可能会这样做:

- (void)scenario2 {

    PGView *viewA = [PGView new];
    viewA.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:viewA];

    PGView *viewB = [PGView new];
    viewB.backgroundColor = [UIColor blackColor];
    [self.view addSubview:viewB];

    viewA.translatesAutoresizingMaskIntoConstraints = NO;
    viewB.translatesAutoresizingMaskIntoConstraints = NO;

    NSDictionary *views = NSDictionaryOfVariableBindings(viewA, viewB);

    // note, I added `|-100-` to say it's 100 points from the top (and 100 tall)

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"V:|-100-[viewB(==100)]" options:0 metrics:nil views:views]];

    // likewise, to say 200 points from the left (and 100 wide)

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"H:|-200-[viewB(==100)]" options:0 metrics:nil views:views]];

    // again, 100 from the top (as well as 200 tall)

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[viewA(==200)]" options:0 metrics:nil views:views]];

    // and 100 from the left
    // but I've removed width definition, as we'll use your multiplier constraint below

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[viewA]" options:0 metrics:nil views:views]];

    // now that we've removed the width constraint from the above,
    // this should now work fine

    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:viewA attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:viewB attribute:NSLayoutAttributeWidth multiplier: 3.0f constant: 0.0f];
    [self.view addConstraint:constraint];

    // no point in doing this, because constraints haven't yet been applied
    //
    // NSLog(@"%@",viewA);
    // NSLog(@"%@",viewB);
}

// if you want to see where they are, you could do that here:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    for (UIView *view in self.view.subviews) {
        NSLog(@"%@", view);
    }
    NSLog(@"-----");
}

关于ios - 以编程方式自动布局不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38801693/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap