【发布时间】:2014-09-17 13:42:37
【问题描述】:
即使使用 ios8,xCode 5 上也不存在此问题。
绿色方块必须在红色方块中,因为 greenView 是红色视图的子视图。但是用 xCode6 构建 greenView 的位置与其父级无关。
- (void)viewDidLoad {
[super viewDidLoad];
/**** 1 - REDVIEW, THE CONTAINER *****/
UIView *redView = [UIView new];
redView.backgroundColor = [UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView];
UIView *spaceView = [UIView new];
spaceView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:spaceView];
NSDictionary *views = @{@"spaceView" : spaceView,
@"redView": redView};
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[spaceView]|"
options: 0
metrics:0
views:views]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[spaceView][redView]|"
options: NSLayoutFormatAlignAllRight | NSLayoutFormatAlignAllLeft
metrics:0
views:views]];
//CENTER VERTICALY
NSLayoutConstraint *constraint = [NSLayoutConstraint
constraintWithItem:redView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:spaceView
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0];
[self.view addConstraint:constraint];
/**** 1 - GREENBUTTON, IN THE CONTAINER *****/
UIButton *greenButton = [[UIButton alloc] init];
greenButton.backgroundColor = [UIColor greenColor];
greenButton.translatesAutoresizingMaskIntoConstraints = NO;
[greenButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[redView addSubview:greenButton];
views = @{@"button" : greenButton};
[redView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[button(35)]|"
options: 0
metrics:0
views:views]];
}
问题出在哪里? (您可以复制并粘贴这些行来尝试)
【问题讨论】:
标签: autolayout constraints ios8 parent nslayoutconstraint