1

さて、私はこの問題で立ち往生しています。カスタムクラス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)]];
    }
4

1 に答える 1

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)]];
于 2013-02-25T17:53:57.890 に答える