UIScrollView在关闭模态视图控制器后立即向下移动44px(相等的导航栏高度)

时间:2013-08-25 09:24:41

标签: iphone ios cocoa-touch uiscrollview autolayout

我有一个带导航栏的UIViewController。我的UIScrollView包含一个UIButtons列表以编程方式添加。我使用AutoLayout将scrollview及其按钮列表添加为下面的代码。

完美无缺,直到显示和解除模态视图。取消模态视图后,滚动视图中的按钮列表向下移动44px(相等的导航栏高度)。 当我尝试删除导航栏时,一切正常。

我搜索了以下问题,但他们不是我的情况:

Contents of UIScrollView shifted after dismissing ModalViewController

Dismiss modal view changes underlying 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];

请注意,scrollview已添加到self.viewIcon,它是使用AutoLayout在故事板中添加为self.view的子视图

谢谢

1 个答案:

答案 0 :(得分:0)

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

在这里寻找解决方案:Shifting view after displaying modal - possibly AutoLayout related