2013-02-24 22 views
1

好的,所以我被困在這個問題上。 我正在抓取2-15個標題(NSStrings)的數組,該數組傳遞到我的自定義類TriButtonGroup中,以變成一行/多行按鈕!按鈕組(UIButton的子類)將水平排列,如果它們都適合!只要所有標題都適合按鈕,就可以運行JUST FINE!但是如果水平空間不足,我需要TriButtonGroup將一些按鈕移動到新的一行。請參閱附件以獲得更好的細節。創建一個自動佈局按鈕組

enter image description here

enter image description here

enter image description here

這是我的代碼,現在,它不處理字符串太長..我只是不能讓我的頭的情況在這附近! :(

int numButtons = [titleArray count]; 
     int counter = 0; 
     TriButtonCell *previousButton = nil; 

     for(NSString *title in titleArray) { 

      TriButtonCell *button = [[TriButtonCell alloc] initWithFrame:CGRectMake(0,0,0,0)]; 
      [button setTitle:title forState:UIControlStateNormal]; 
      [button setTag:1250+counter]; 
      [button addTarget:nil action:@selector(pushButton:) forControlEvents:UIControlEventTouchDown]; 
      [button.titleLabel setNumberOfLines:1]; 
      [button setTranslatesAutoresizingMaskIntoConstraints:NO]; 
      [self addSubview:button]; 

      //Add the correct sizing! 
      if(previousButton != nil) { 
       [self addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:previousButton attribute:NSLayoutAttributeRight multiplier:1.0f constant:0.0f]]; 
       [self addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:previousButton attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]]; 
      } else { 
       //This is the first button! Set the correct width! 
       [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[button]" options:0 metrics:metricsDictionary views:NSDictionaryOfVariableBindings(button)]]; 
      } 
      [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button(60)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]]; 

      previousButton = button; 

      counter++; 
     } 

     //Set the distance to the last button! 
     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[previousButton]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(previousButton)]]; 
    } 

回答

1

我能想到的將要求各TriButtonCellintrinsicContentSize和工作了手動的按鈕適合於行,每一行分別創建約束的唯一的事情。

喜歡的東西這個(註銷我的頭頂,所以它可能不是100%準確)

CGFloat rowWidth = 0.0; 
NSView *previousButton; 

for (NSString *title in titleArray) { 
    TriButtonCell *button = [[TriButtonCell alloc] initWithFrame:NSZeroRect]; 
    // rest of button init here 

    NSSize intrinsicSize = [button intrinsicContentSize]; 

    if(rowWidth + intrinsicSize.width < [self bounds].size.width) { 
     if(previousButton != nil) { 
      NSDictionary *viewsDict = {@"previousButton":previousButton, @"currentButton": button}; 
      [self addConstrints:[NSLayoutConstraint constraintsWithVisualFormat:@"[previousButton][currentButton(==previousButton)" 
                     options:0 
                     metrics:nil 
                     views:viewsDict]; 
     } else { 
      //This is the first button! Set the correct width! 
      [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[button]" 
                     options:0 
                     metrics:nil 
                      views:NSDictionaryOfVariableBindings(button)]]; 
     } 
    } else { 
     // We need a new row. 
     rowWidth = 0.0; 

     NSDictionary *viewsDict = {@"previousButton":previousButton, @"currentButton": button}; 
     // Connect the top of the current button to the bottom of the button of the previous row 
     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[previousButton][currentButton]" 
                    options:0 
                    metrics:nil 
                     views:viewsDict]; 

     // Also connect it to the left side of the parent view 
     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[currentButton]" 
                    options:0 
                    metrics:nil 
                     views:viewsDict]; 
    } 

    previousButton = button; 
    counter++; 

} 

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:"@[previousButton]|" 
       options:0 
       metrics:nil 
        views:NSDictionaryOfVariableBindings(previousButton)]]; 
+0

使用排序你的答案上面:)對不起,漫長的等待接受的答案來解決它! – Hjalmar 2014-02-05 11:56:53