【发布时间】:2015-08-23 20:43:21
【问题描述】:
我正在学习 UIAutolayout。在这篇文章中,我学习了如何将视图添加到超级视图,然后提供约束。我打算在屏幕上有两个视图,给它们红色和蓝色。由于约束同时影响超级视图和子视图(如格式字符串所示),我已将约束添加到超级视图。(这是错误的???) 我在 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] ;
问题:我只得到蓝色视图作为屏幕上的输出。我没有得到红色的。我已经在 iPad 和 iPhone 模拟器上测试过,但结果是一样的。没有更多的代码可以给出。我正在使用 Xcode 5 和 iOS 7(嗯,因为我正在学习)。谢谢。
【问题讨论】:
标签: ios objective-c iphone ipad ios-autolayout