2

ナビゲーションバー付きの UIViewController があります。UIButtons のリストを含む私の UIScrollView は、プログラムで追加されます。AutoLayout を使用して、スクロールビューとそのボタン リストを以下のコードとして追加しました。

モーダル ビューが表示され、閉じられるまで、完全に機能します。モーダル ビューを閉じると、スクロール ビューのボタン リストが 44px 下に移動します (ナビゲーション バーの高さと同じです)。 ナビゲーション バーを削除しようとすると、すべてが正しく機能します。

以下の質問を検索しましたが、私の場合ではありません。

ModalViewController を閉じた後、UIScrollView の内容が移動しました

UIScrollView の基礎となるモーダル ビューの変更を無視する

AutoLayoutを使用してuiscrollviewを追加する私のコード:

UIScrollView* scrollView = [UIScrollView new];
[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];

//calculate contentSize based on the button count
int buttonCount = 10;
CGSize contentSize = CGSizeMake(ICON_X*2 + (ICON_WIDTH + ICON_SPACE) * buttonCount - ICON_SPACE, ICON_HEIGHT);

//calculate trailing offset for the first button
CGFloat trailingOffset = contentSize.width - (ICON_X + ICON_WIDTH);

//add list of button to the scrollview
for (int i = 0; i < buttonCount; i++) {

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTranslatesAutoresizingMaskIntoConstraints:NO];

    //set button image
    //set button target

    //set tag
    button.tag = i;

    //add to the scroll view
    [scrollView addSubview:button];

    //calculate the button's frame using AutoLayout
    NSDictionary *dict = NSDictionaryOfVariableBindings(button);
    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-%g-[button(%d)]-%g-|", ICON_X + (ICON_WIDTH + ICON_SPACE)*i, ICON_WIDTH, trailingOffset] options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];
    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat: @"V:|-%g-[button(%d)]-%g-|", ICON_Y, ICON_HEIGHT, ICON_Y] options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];

    //update the trailing offset for the next button
    if (i == buttonCount - 2) //the next one is the last button
        trailingOffset = ICON_X;
    else
        trailingOffset -= (ICON_SPACE + ICON_WIDTH);
}

//add the scrollView to self.viewIcon
//self.viewIcon is a UIView which was added as a subview of self.view in the storyboard using AutoLayout
[self.viewIcon addSubview:scrollView];

//calculate the scrollview's frame using AutoLayout
//contraint autolayout
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(1)-[scrollView(58)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)];
NSArray *horizontalContraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)];
[self.viewIcon addConstraints:verticalConstraints];
[self.viewIcon addConstraints:horizontalContraints];

AutoLayout を使用して、ストーリーボードで self.view のサブビューとして追加された self.viewIcon に scrollview が追加されていることに注意してください。

ありがとうございました

4

1 に答える 1

0

iOS 6 でも同じ問題があります。iOS7GM では問題が解決されているようです。

解決策はこちらをご覧ください:モーダルを表示した後にビューをシフトする - おそらくAutoLayout関連

于 2013-09-13T13:27:45.777 に答える