X $ C $ 6 - 在其他自动版式视图自动布局视图 [英] Xcode6 - Autolayout view in an other autolayout view

查看:160
本文介绍了X $ C $ 6 - 在其他自动版式视图自动布局视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题没有对X code 5,即使iOS8上存在。

This problem didn't exist on xCode 5 even with ios8.

绿色方块必须在红场导致绿景是红色视图的子视图。但随着X $ C $ 6 建绿景的位置是不是相对以其父。

The green square have to be in the red square cause the greenView is a subview of the red view. But built with xCode6 the position of the greenView is not relative to its parent.

- (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]];



}

在哪里不舒服?
(您可以复制和粘贴这些线试试)

Where is the trouble? (you can copy and paste those line to try)

推荐答案

中定义的greenButton相对于它的父被定位的限制。问题在于缺少的垂直位置的约束。另外请注意,您使用Visual语法导致冲突的约束:

The constraints defined are positioning the greenButton relative to its superview. The issue lies in the missing the vertical position constraint. Also note that the visual syntax you used resulted in conflicting constraints:


  • 0距离领先

  • 0尾随距离(我删除了这一项,使其工作)

  • 35宽度

  • witdth的上海华= 35;

如果您使用的greenButton以下的限制,它会被放在左上角其父。

If you use the following constraints for the greenButton, it will be placed top-left on its parent.

[redView addConstraints:[NSLayoutConstraint
                             constraintsWithVisualFormat:@"H:|[button(35)]"
                             options: 0
                             metrics:0
                             views:views]];

[redView addConstraints:[NSLayoutConstraint
                             constraintsWithVisualFormat:@"V:|[button(35)]"
                             options:0
                             metrics:0
                             views:views]];

这篇关于X $ C $ 6 - 在其他自动版式视图自动布局视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆