0

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

enter image description here

Michael Choi
  • 285
  • 1
  • 3
  • 15

1 Answers1

0

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];
Michael Choi
  • 285
  • 1
  • 3
  • 15