简体   繁体   English

以编程方式自动布局垂直iOS时出错

[英]Error in programmatically Auto-layout vertically ios

I am creating view programmatically, In it there is two sub views, I have set height and width constraint for that, what I want is like this, 我正在以编程方式创建视图,在其中有两个子视图,为此我设置了高度和宽度约束,我想要的是这样,

UIView (variable height)
[10px gap]
UIView (fix height 40)

but I got: 但是我得到了:

在此处输入图片说明

My code is: 我的代码是:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *button1, *button2 ;
    button1=[UIView new];
    button2=[UIView new];
    button1.backgroundColor=[UIColor redColor];
    button2.backgroundColor=[UIColor yellowColor];
    button1.translatesAutoresizingMaskIntoConstraints=button2.translatesAutoresizingMaskIntoConstraints=NO;

    [self.view addSubview:button1];
    [self.view addSubview:button2];
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(button1,button2);

    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button1]-|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:viewsDictionary];

    [self.view addConstraints:constraints];

    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[button1]-10-[button2]-|"
                                                          options: NSLayoutFormatAlignAllLeft
                                                          metrics:nil
                                                            views:viewsDictionary];

    [self.view addConstraints:constraints];
}

Edit Second Try 编辑 第二次尝试

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *button1, *button2 ;
    button1=[UIView new];
    button2=[UIView new];
    button1.backgroundColor=[UIColor redColor];
    button2.backgroundColor=[UIColor yellowColor];
    button1.translatesAutoresizingMaskIntoConstraints=button2.translatesAutoresizingMaskIntoConstraints=NO;

    [self.view addSubview:button1];
    [self.view addSubview:button2];

 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1
                                                                 attribute:NSLayoutAttributeLeading
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.view 
                                                                 attribute:NSLayoutAttributeLeft
                                                                multiplier:1.0
                                                                  constant:25.0]];

//    [self.view  addConstraint:[NSLayoutConstraint constraintWithItem:button1
//                                                                 attribute:NSLayoutAttributeWidth
//                                                                 relatedBy:NSLayoutRelationEqual
//                                                                    toItem:nil
//                                                                 attribute:NSLayoutAttributeNotAnAttribute
//                                                                multiplier:1.0
//                                                                  constant:100]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeRight
                                                         multiplier:1.0
                                                           constant:-25.0]];


    [self.view  addConstraint:[NSLayoutConstraint constraintWithItem:button1
                                                                 attribute:NSLayoutAttributeTop
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.view 
                                                                 attribute:NSLayoutAttributeTop
                                                                multiplier:1.0
                                                                  constant:30]];

    [self.view  addConstraint:[NSLayoutConstraint constraintWithItem:button1
                                                                 attribute:NSLayoutAttributeBottom
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:button2
                                                                 attribute:NSLayoutAttributeBottom
                                                                multiplier:1.0
                                                                  constant:-30.0]];






    //// Yellow

    /// Left
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button2
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeLeft
                                                         multiplier:1.0
                                                           constant:25.0]];

    //Right
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button2
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeRight
                                                         multiplier:1.0
                                                           constant:-50.0]];




    [self.view  addConstraint:[NSLayoutConstraint constraintWithItem:button2
                                                           attribute:NSLayoutAttributeTop
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:button1
                                                           attribute:NSLayoutAttributeTop
                                                          multiplier:1.0
                                                            constant:30]];

//    [self.view  addConstraint:[NSLayoutConstraint constraintWithItem:button2
//                                                           attribute:NSLayoutAttributeBottom
//                                                           relatedBy:NSLayoutRelationEqual
//                                                              toItem:self.view
//                                                           attribute:NSLayoutAttributeBottom
//                                                          multiplier:1.0
//                                                            constant:-30.0]];

    [self.view  addConstraint:[NSLayoutConstraint constraintWithItem:button2
                                                                 attribute:NSLayoutAttributeHeight
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:nil
                                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                                multiplier:1.0
                                                                  constant:100]];
}

In this I got: 在此我得到:

在此处输入图片说明

Regular views don't have any intrinsic size, and you've given the system no hints about how big the views should be, so button 1 gets laid out first with at least 10 points to spare, and view 2 ends up being 0 points high and 0 points wide. 常规视图没有任何内在大小,并且您也没有给系统任何有关视图应该有多大的提示,因此按钮1的布局首先要保留至少10点,视图2最终会得到0点高0点宽

To correct this, make sure that you give both views some horizontal rules, not just one of the views. 若要更正此问题,请确保为两个视图都提供一些水平规则,而不仅仅是一个视图。 Secondly, make sure you give the system some idea about height. 其次,确保给系统一些关于高度的想法。 If you want the views to be equal sizes, you need to tell the system that. 如果希望视图大小相等,则需要告诉系统。 Add another horizontal constraint for button 2: 为按钮2添加另一个水平约束:

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button2]-|" options:0 metrics:0 views:viewsDictionary];
[[self view] addConstraints:constraints];

Then add a height constraint for the view, in this case, adjust your vertical constraints to make the views equal heights by adding the (==button1) size information: 然后为视图添加高度约束,在这种情况下,通过添加(==button1)大小信息来调整垂直约束以使视图等于高度:

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[button1]-10-[button2(==button1)]-|"
                                                      options:0
                                                      metrics:nil
                                                        views:viewsDictionary];

Now you should see two views, red on top, yellow on bottom, that take up an equal amount of vertical space, have 10p space between and stretch to 20 points of each side of the container view. 现在您应该看到两个视图,顶部为红色,底部为黄色,这两个视图占据了相等的垂直空间,它们之间有10p的间距,并延伸到容器视图的每一侧20个点。

To create these same constraints manually (which I don't recommend), you would do something like this: 要手动创建这些相同的约束(我不建议这样做),您可以执行以下操作:

UIView* view = [self view]; // for brevity
NSMutableArray* manualConstraints = [NSMutableArray array];
NSLayoutConstraint* b1_top = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1 constant:20];
[manualConstraints addObject:b1_top];
NSLayoutConstraint* b1_left = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeLeading multiplier:1 constant:20];
[manualConstraints addObject:b1_left];
NSLayoutConstraint* b1_right = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTrailing multiplier:1 constant:-20];
[manualConstraints addObject:b1_right];
NSLayoutConstraint* b1_bottom = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:button2 attribute:NSLayoutAttributeTop multiplier:1 constant:-10];
[manualConstraints addObject:b1_bottom];
NSLayoutConstraint* b2_left = [NSLayoutConstraint constraintWithItem:button2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeLeading multiplier:1 constant:20];
[manualConstraints addObject:b2_left];
NSLayoutConstraint* b2_right = [NSLayoutConstraint constraintWithItem:button2 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTrailing multiplier:1 constant:-20];
[manualConstraints addObject:b2_right];
NSLayoutConstraint* b2_bottom = [NSLayoutConstraint constraintWithItem:button2 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeBottom multiplier:1 constant:-20];
[manualConstraints addObject:b2_bottom];
NSLayoutConstraint* b2_height = [NSLayoutConstraint constraintWithItem:button2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40];
[manualConstraints addObject:b2_height];
// Add all constraints
[view addConstraints:manualConstraints];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM