Search code examples
iosuiscrollviewios6autolayout

iOS: Change contentsize of a UIScrollview using auto layout


I'm implementing a form inside a UIScrollView. I'm trying to add some space at the bottom of the content of a scroll view when the keyboard is opened so the user can see all the fields. I put the form view inside the UISCrollView adding all the needed constraints with the following code:

 [_infoView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_infoView(1730)]" options:0 metrics:nil views:views]];

[_scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_infoView]|" options:0 metrics:nil views:views]];

[_scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_infoView]|" options:0 metrics:nil views:views]];

[_scrollView addConstraint:[NSLayoutConstraint constraintWithItem:_infoView
                                                        attribute:NSLayoutAttributeCenterX
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:_scrollView
                                                        attribute:NSLayoutAttributeCenterX
                                                       multiplier:1
                                                         constant:0]];

As you can see, I specify in the first line the height of the form and the scrollview adapts its content size automatically. Now I want to increase the height of the form, so I've tried to reset the constraint for the height with a greater one but it won't work. Then I've tried to use the [_scrollView setContentSize:] method but this also won't work. Can anyone help me please?


Solution

  • I am not sure where you add the above code, but the below should solve your problem

    In Your init Function, add the below:

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center addObserver:self selector:@selector(noticeShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];
            [center addObserver:self selector:@selector(noticeHideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
    

    Add the below to your .h

    CGSize keyboardSize;
    int keyboardHidden;      // 0 @ initialization, 1 if shown, 2 if hidden
    

    Add the below to your .m

    -(void) noticeShowKeyboard:(NSNotification *)inNotification {
        keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        keyboardHidden = 1;
        [self layoutSubviews];        // Not sure if it is called automatically, so I called it
    
    
    }
    -(void) noticeHideKeyboard:(NSNotification *)inNotification {
        keyboardHidden = 2;
        [self layoutSubviews];       // Not sure if it is called automatically, so I called it
    
    }
    
    - (void) layoutSubviews
    {
        [super layoutSubviews];
    
        if(keyboardHidden == 1) {
            scrollview.frame = CGRectMake(scrollview.frame.origin.x, scrollview.frame.origin.y, scrollview.frame.size.width, scrollview.frame.size.height + keyboardSize.height);
        }
        else if(keyboardHidden == 2) {
            scrollview.frame = CGRectMake(scrollview.frame.origin.x, scrollview.frame.origin.y, scrollview.frame.size.width, scrollview.frame.size.height - keyboardSize.height);
        }
    }
    

    I overrided layoutsubviews, now I think it should work.