在iOS上以编程方式添加约束

时间:2014-10-27 18:29:45

标签: autolayout constraints

所以我试图让第一个和第三个按钮占据标签栏的其余部分并让第二个按钮保持恒定宽度。我越来越近但我似乎无法得到它。这是我第一次以编程方式添加约束,所以我欢迎建设性的批评。这是代码。在图像上,每种颜色都是一个按钮。

    - (void)setUpTabbarProperties {
    [self.tabBar setHidden: YES];
    [self.customTabBarView setTranslatesAutoresizingMaskIntoConstraints: NO];
    [self.firstButton setTranslatesAutoresizingMaskIntoConstraints: NO];
    [self.secondButton setTranslatesAutoresizingMaskIntoConstraints: NO];
    [self.thirdButton setTranslatesAutoresizingMaskIntoConstraints: NO];
    [self.view addSubview: self.customTabBarView];


    // This will add the constraints
    NSDictionary *viewDictionary = [NSDictionary dictionaryWithObjectsAndKeys: self.customTabBarView, @"customTabBar",
                                                                               self.firstButton, @"firstButton",
                                                                               self.secondButton, @"secondButton",
                                                                               self.thirdButton, @"thirdButton", nil];

    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat: @"H:|-0-[customTabBar]-0-|"
                                                                             options: 0
                                                                             metrics: nil
                                                                               views: viewDictionary];

    NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat: @"V:[customTabBar]-0-|"
                                                                           options: 0
                                                                           metrics: nil
                                                                             views: viewDictionary];

    NSArray *heightContraints = [NSLayoutConstraint constraintsWithVisualFormat: @"V:[customTabBar(50)]"
                                                                        options: 0
                                                                        metrics: nil
                                                                          views: viewDictionary];

    NSArray *buttonHConstraints = [NSLayoutConstraint constraintsWithVisualFormat: @"H:|-0-[firstButton]-0-[secondButton]-0-[thirdButton]-0-|"
                                                                          options: 0
                                                                          metrics: nil
                                                                            views: viewDictionary];

    NSArray *firstButtonVConstraints = [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-0-[firstButton]-0-|"
                                                                         options: 0
                                                                         metrics: nil
                                                                           views: viewDictionary];

    NSArray *secondButtonVConstraints = [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-0-[secondButton]-0-|"
                                                                          options: 0
                                                                          metrics: nil
                                                                            views: viewDictionary];

    NSArray *thirdButtonVConstraints = [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-0-[thirdButton]-0-|"
                                                                          options: 0
                                                                          metrics: nil
                                                                            views: viewDictionary];

    NSArray *secondButtonWithConstraint = [NSLayoutConstraint constraintsWithVisualFormat: @"H:[secondButton(65)]"
                                                                                  options: 0
                                                                                  metrics: nil
                                                                                    views: viewDictionary];

    [self.view addConstraint: [NSLayoutConstraint constraintWithItem: self.secondButton
                                                           attribute: NSLayoutAttributeCenterY
                                                           relatedBy: NSLayoutRelationEqual
                                                              toItem: self.customTabBarView
                                                           attribute: NSLayoutAttributeCenterY
                                                          multiplier: 0
                                                            constant: 0]];

    [self.view addConstraints: horizontalConstraints];
    [self.view addConstraints: verticalConstraints];
    [self.view addConstraints: heightContraints];
    [self.view addConstraints: buttonHConstraints];
    [self.view addConstraints: firstButtonVConstraints];
    [self.view addConstraints: secondButtonVConstraints];
    [self.view addConstraints: thirdButtonVConstraints];
    [self.view addConstraints: secondButtonWithConstraint];
}

这里看起来像什么

enter image description here

1 个答案:

答案 0 :(得分:0)

我错过了约束。我试图使用另一个并且它没有工作,但这个确实有用。

        NSArray *firstAndThirdButtonConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[firstButton(==thirdButton)]"
                                                                                     options:0
                                                                                     metrics:nil views:viewDictionary];