gpt4 book ai didi

ios - 使用 Autolayout Visual Format Language 将 UIViews 排成一行

转载 作者:行者123 更新时间:2023-11-29 01:30:04 24 4
gpt4 key购买 nike

所以,我想使用 Autolayout Visual Format Language 将 4 个 UIViews 水平放置(从上到下,如键盘行)...但是对于出于某种原因,它似乎没有按预期工作,因为我的观点似乎在彼此之上的堆栈中结束......可能是由于视觉格式中的错误......

所以我有 4 个 UIViews:

UIView *rowOne = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[rowOne setBackgroundColor:[UIColor blueColor]];
[rowOne setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:rowOne];

UIView *rowTwo = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 50, 50)];
[rowTwo setBackgroundColor:[UIColor greenColor]];
[rowTwo setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:rowTwo];

UIView *rowThree = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 50, 50)];
[rowThree setBackgroundColor:[UIColor redColor]];
[rowThree setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:rowThree];

UIView *rowFour = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 50, 50)];
[rowFour setBackgroundColor:[UIColor yellowColor]];
[rowFour setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:rowFour];

我将它们添加到字典中:

NSDictionary *views = NSDictionaryOfVariableBindings(rowOne, rowTwo, rowThree, rowFour);

我创建约束:

NSString *rowsVerticalConstraintsString = @"V:|-[rowOne][rowTwo][rowThree][rowFour]-|";
NSString *rowOneHorizontalConstraintString = @"H:|-[rowOne]-|";
NSString *rowTwoHorizontalConstraintString = @"H:|-[rowTwo]-|";
NSString *rowThreeHorizontalConstraintString = @"H:|-[rowThree]-|";
NSString *rowFourHorizontalConstraintString = @"H:|-[rowFour]-|";

NSArray *rowsVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:rowsVerticalConstraintsString
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:views];

NSArray *rowOneHorizontalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:rowTwoHorizontalConstraintString
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:views];

NSArray *rowTwoHorizontalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:rowOneHorizontalConstraintString
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:views];

NSArray *rowThreeHorizontalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:rowThreeHorizontalConstraintString
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:views];

NSArray *rowFourHorizontalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:rowFourHorizontalConstraintString
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:views];

并将约束添加到 View :

[self.view addConstraints:rowsVerticalConstraints];
[self.view addConstraints:rowOneHorizontalConstraint];
[self.view addConstraints:rowTwoHorizontalConstraint];
[self.view addConstraints:rowThreeHorizontalConstraint];
[self.view addConstraints:rowFourHorizontalConstraint];

我期望的行为是 View 垂直并排放置并水平拉伸(stretch),但它们似乎相互重叠......

知道我需要更改什么吗?

最佳答案

Note:when using auto layout,initWithFrame have no compact

您的布局约束没有高度约束。您可以设置 4 个 View 具有相同的高度

NSString *rowsVerticalConstraintsString = @"V:|-[rowOne(==rowTwo)][rowTwo][rowThree(==rowTwo)][rowFour(==rowTwo)]-|";

然后截图

另外,你可以设置宽度为150,高度50

 NSString *rowsVerticalConstraintsString = @"V:|-[rowOne(50)][rowTwo(50)][rowThree(50)][rowFour(50)]";
NSString *rowOneHorizontalConstraintString = @"H:|-[rowOne(150)]";
NSString *rowTwoHorizontalConstraintString = @"H:|-[rowTwo(150)]";
NSString *rowThreeHorizontalConstraintString = @"H:|-[rowThree(150)]";
NSString *rowFourHorizontalConstraintString = @"H:|-[rowFour(150)]";

然后截图

关于ios - 使用 Autolayout Visual Format Language 将 UIViews 排成一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33546183/

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