【问题标题】:Xcode6 - Autolayout view in an other autolayout viewXcode 6 - 另一个自动布局视图中的自动布局视图
【发布时间】: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


    【解决方案1】:

    定义的约束是相对于其父视图定位greenButton。问题在于缺少垂直位置约束。另请注意,您使用的视觉语法导致约束冲突:

    • 0 距离领先
    • 0 距离尾随(我删除了这个以使其工作)
    • 宽度为 35
    • 宽度的超级视图!= 35;

    如果您对 greenButton 使用以下约束,它将被放置在其父级的左上角。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多