1
好的,所以我被困在這個問題上。 我正在抓取2-15個標題(NSStrings)的數組,該數組傳遞到我的自定義類TriButtonGroup中,以變成一行/多行按鈕!按鈕組(UIButton的子類)將水平排列,如果它們都適合!只要所有標題都適合按鈕,就可以運行JUST FINE!但是如果水平空間不足,我需要TriButtonGroup將一些按鈕移動到新的一行。請參閱附件以獲得更好的細節。創建一個自動佈局按鈕組
這是我的代碼,現在,它不處理字符串太長..我只是不能讓我的頭的情況在這附近! :(
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)]];
}
使用排序你的答案上面:)對不起,漫長的等待接受的答案來解決它! – Hjalmar 2014-02-05 11:56:53