2014-10-27 145 views
0

因此,我試圖讓第一個和第三個按鈕佔據標籤欄的其餘空間,並讓第二個按鈕的寬度不變。我正在接近,但我似乎無法完全理解。這是我第一次以編程方式添加約束,所以我歡迎建設性的批評。這是代碼。在圖像上,每種顏色都是一個按鈕。在iOS上以編程方式添加約束條件

- (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

回答

0

我缺少約束。我試圖使用一個不同的,它不工作,但這一個。

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