2

我有一个带有导航栏的 UIViewController。我的 UIScrollView 包含 UIButtons 列表以编程方式添加。我使用 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];

请注意,滚动视图被添加到 self.viewIcon,它是使用 AutoLayout 在情节提要中作为 self.view 的子视图添加的

谢谢

4

1 回答 1

0

我在iOS 6上也有同样的问题。这个问题似乎在iOS7GM上得到了解决。

在这里寻找解决方案:显示模式后移动视图 - 可能与 AutoLayout 相关

于 2013-09-13T13:27:45.777 回答