gpt4 book ai didi

ios - View 未扩展以匹配给定的约束

转载 作者:行者123 更新时间:2023-11-28 21:40:43 27 4
gpt4 key购买 nike

我正在学习 UIAutolayout。在这里,我学习了如何向父 View 添加 View ,然后提供约束。我计划在屏幕上有两个 View ,给它们红色和蓝色。由于约束同时影响父 View 和 subview (如格式字符串所示),我已将约束添加到父 View 中。(这是错误的吗???) 我在 viewDidLoad 函数中输入了以下代码。

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


UIView * view1 = [[UIView alloc] init];
[view1 setBackgroundColor:[UIColor redColor] ] ;


UIView *view2 = [[UIView alloc] init];
[view2 setBackgroundColor: [UIColor blueColor]] ;


//Adding the view to the main view.
[self.view addSubview:view1] ;

[self.view addSubview:view2] ;


[view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[view2 setTranslatesAutoresizingMaskIntoConstraints:NO];

//Making the namemap for sending to the function
NSDictionary *nameMap = @{@"view1":view1,@"view2":view2} ;


//Creating the constraint below

NSArray * horizontalContraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[view2]-60-[view1]-30-|" options:0 metrics:nil views:nameMap] ;

NSArray *view1Height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[view1]-30-|" options:0 metrics:nil views:nameMap] ;

NSArray *view2Height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[view2]-30-|" options:0 metrics:nil views:nameMap] ;


//Adding the constraints
[self.view addConstraints:horizontalContraints] ;

[self.view addConstraints:view2Height] ;

[self.view addConstraints:view1Height] ;

问题:我在屏幕上只得到蓝色 View 作为输出。我没有得到红色的。我已经在 iPad 和 iPhone 模拟器上测试过这个,但结果是一样的。没有更多的代码可以提供。我正在使用 Xcode 5 和 iOS 7(好吧,因为我正在学习)。谢谢。

最佳答案

问题出在您的“horizo​​ntalContraints”上。正如 paresh 所说,您需要指定 View 的宽度。

因此,将“horizo​​ntalContraints”更改为

NSArray *horizontalContraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[view2]-60-[view1(==view2)]-30-|" options:0 metrics:nil views:dict];

会给你想要的输出。

修改后你的整体如下。

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UIView * view1 = [[UIView alloc] init];
[view1 setBackgroundColor:[UIColor redColor]];

UIView *view2 = [[UIView alloc] init];
[view2 setBackgroundColor:[UIColor blueColor]];

[view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[view2 setTranslatesAutoresizingMaskIntoConstraints:NO];

//Adding the view to the main view.
[self.view addSubview:view1];
[self.view addSubview:view2];

NSDictionary *dict =[[NSDictionary alloc] init];
dict = @{@"view1":view1, @"view2":view2};

NSArray *horizontalContraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[view2]-60-[view1(==view2)]-30-|" options:0 metrics:nil views:dict];

NSArray *view1Height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[view1]-30-|" options:0 metrics:nil views:dict];

NSArray *view2Height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[view2]-30-|" options:0 metrics:nil views:dict];

[self.view addConstraints:horizontalContraints];
[self.view addConstraints:view2Height];
[self.view addConstraints:view1Height];
}

希望对您有所帮助。 :)

关于ios - View 未扩展以匹配给定的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32171341/

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