在其他自动布局视图中的自动布局视图

在其他自动布局视图中的自动布局视图

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

问题描述

这个问题在xCode 5上不存在,即使使用ios8。

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

绿色方块必须在红色方块中 ,因为greenView是红色视图的子视图。但是用xCode6构建 greenView的位置不是相对的

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超视。问题在于缺少垂直位置约束。另请注意,您使用的可视化语法会导致冲突的约束:

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 distance to leading
  • 0 distance to trailing (I removed this one to make it work)
  • width of 35
  • superview of 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]];

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

08-14 04:09