3

このトピック (およびiOS 6 リリース ノート)に関するスタック オーバーフロー スレッドをたくさん見てきましたが、ここでは役に立たなかった、または理解できませんでした。私が持っているのは、ページングで幅の 2 倍にスクロールしたいサイズ (244, 46) のスクロール ビューです。

ボタンのリストと制約のリスト、および削除されない紫色の制約を示すストーリーボード

私の絵コンテでは、スクロール ビューとそのサブビュー (の行UIButtons) をレイアウトしましたsuperview。それらを「Leading space to button」(連続するボタン間の水平方向のスペース) に置き換えました。コードでは、ボタンの translatesAutoresizingMaskIntoConstraints を NO に設定しています。

ただし、スクロールビューをスクロールすることはできません。スクロールビューの境界の右側に何かがあり、手の届かないところにあることを示すためにバウンスするだけです。私が間違っていること、またはこれを機能させるために他に何か必要なことはありますか?

ああ、また、Interface Builder の最後の表示ボタン (行の最後のボタンではない) に「スーパービューへsuperviewの末尾のスペース」制約と、削除されたままにならない最初のボタンの「先頭のスペースへ」の制約もありました。 (画像の紫色の制約を参照してください)。迷惑なことに、リストの別の場所に表示されました。これはなぜですか? また、コンテンツのサイズが適切に設定されていないのはなぜですか?

御時間ありがとうございます!

4

1 に答える 1

0

@robmayoff の提案を受けて、ボタンの作成をコードに移したところ、奇跡的に機能しました。(残念ながら、自動レイアウトは私にはまだギリシャ語ですが、何とかそれを管理できました。) 将来誰かがボタンのスクロール リストを作成する必要がある場合に備えて、contentSize明示的に設定する必要のない方法を次に示します。 IBより少し醜い。

self.toolbarScrollView.translatesAutoresizingMaskIntoConstraints = NO;

NSMutableArray *toolbarItems = [NSMutableArray array];

//Unfortunately, you'll have to set the contentWidth manually - in viewDidLoad
// the frames aren't set yet. 47.0 is the size (38) + margin (9) of one of my
// buttons.
CGFloat x = contentWidth - 47.0;

//This code lets you page the scroll view with more spacing between items on 
//different pages.
int itemCountPerPage = 5;
NSArray *images = @[/*...titles of images...*/];
SEL selectors[10] = {
    //button selectors
};

UIButton *lastButton = nil;

for (int i = 0; i < [images count]; i++)
{
    NSString *imageName = [images objectAtIndex:i];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.translatesAutoresizingMaskIntoConstraints = NO;
    [button setImage:[UIImage imageNamed:imageName maskedWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    button.showsTouchWhenHighlighted = YES;
    if (selectors[i] != NULL)
        [button addTarget:self action:selectors[i] forControlEvents:UIControlEventTouchUpInside];
    [self.toolbarScrollView addSubview:button];
    [toolbarItems addObject:button];

    if (lastButton)
    {
        NSDictionary *dict = NSDictionaryOfVariableBindings(lastButton, button);
        [self.toolbarScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[lastButton(==38)]-%g-[button(==38)]-%g-|", (i % itemCountPerPage == 0 ? 18.0 : 9.0), x] options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];
        [self.toolbarScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-4-[button(==38)]-4-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];
    }
    else
    {
        NSDictionary *dict = NSDictionaryOfVariableBindings(button);
        [self.toolbarScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-9-[button(==38)]-%g-|", x] options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];
        [self.toolbarScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-4-[button(==38)]-4-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];          
    }

    x -= (i % itemCountPerPage == itemCountPerPage - 1 ? 56.0 : 47.0);  //Extra space to allow for paging
    lastButton = button;
}

単純なスクロールビューで多くの問題が発生したため、これが誰かの助けになることを願っています! ご協力いただきありがとうございます。

于 2013-06-27T03:22:19.023 に答える