2
votes

I have an UIViewController with a navigation bar. My UIScrollView which contains a list of UIButtons is added programmatically. I used AutoLayout to add the scrollview and its buttons list as the code below.

It works perfectly until there is a modal view is showed and dismissed. After the modal view dismissed, the button list in the scrollview is shifted down 44px (equal navigation bar height). When I try removing the navigation bar, everything work correctly.

I searched in the below questions but they aren't my case:

Contents of UIScrollView shifted after dismissing ModalViewController

Dismiss modal view changes underlying UIScrollView

My code to add an uiscrollview using AutoLayout:

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];

Please notice that the scrollview is added to self.viewIcon which was added as a subview of self.view in the storyboard using AutoLayout

Thank you

1
Could you please post some code?tharris
@tharris I edited my question with some code. Thank you :)Tien Nguyen
Even though I used AutoLayout to add button list to the scrollview, the issue still remain. Please see my updated code that is using auto layout.Tien Nguyen

1 Answers

0
votes

I have also the same problem on iOS 6. The problem seems to be solved on iOS7GM.

Look here for a solution : Shifting view after displaying modal - possibly AutoLayout related