さて、私はこの問題で立ち往生しています。カスタムクラスTriButtonGroupに渡されて1行/複数行のボタンに変換される2〜15個のタイトル(NSStrings)の配列をフェッチしています!ボタンのグループ(UIButtonのサブクラス)は、すべてが収まる場合は水平に並んでいます!これは、すべてのタイトルがボタンの内側に収まる限り、問題なく機能します。ただし、水平方向のスペースが不足している場合は、一部のボタンを新しい行に移動するために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)]];
}