Adding Constraints Programmatically on iOS

大憨熊 提交于 2019-12-12 02:05:56

问题


So I'm trying to get the first and third button to take up the rest of the space of the tab bar and have the second button be a constant width. I'm getting close but I can't seem to quite get it. This is my first time adding constraints programmatically so I welcome constructive criticism. Here is the code. On the image each color is a button.

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

and here what it looks like


回答1:


I was missing the constraint. I was trying to use a different one and it didn't work but this one did.

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


来源:https://stackoverflow.com/questions/26594369/adding-constraints-programmatically-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!