Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move UIScrollView when keyboard comes into place

[Edit:] The problem has been solved. I did not have my delegates linked properly in UIBuilder. Code is good!

I am trying to resize a scrollview when the keyboard appears. I went to the developer docs and found this information.

http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW1

On the left "Managing the keyboard".

In the documentation it shows a bit of code to detect the size of the keyboard and then to resize a UIScrollView. I have placed an NSLog message in the code for function - (void)keyboardWasShown:(NSNotification*)aNotification so I see that the function is actually being called, but when I try to to NSLog the kbSize.height it is always valued at 0.

Why does the code that apple provide for this purpose not work?

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}
like image 658
thenetimp Avatar asked Dec 21 '11 13:12

thenetimp


People also ask

How do I move ScrollView up when a keyboard appears Swiftui?

You need to add a ScrollView and set a bottom padding of the size of the keyboard so the content will be able to scroll when the keyboard appears. All you have to do now is to embed your content inside the custom ScrollView .

How do I scroll up when keyboard appears in iOS?

Handling the Keyboard Appearing Notification There are two things to do in keyboardWasShown to scroll the text view. First, set the text view's content inset so the bottom edge is the keyboard's height. Second, set the scroll indicator insets to the text view's content inset.

Which component is used to move UI up when keyboard appears?

This component will automatically adjust its height, position, or bottom padding based on the keyboard height to remain visible while the virtual keyboard is displayed.


1 Answers

You may want to try the highly recommended "TPKeyboardAvoidingScrollView", available from: https://github.com/michaeltyson/TPKeyboardAvoiding

Works like a charm...

like image 114
Reuven Avatar answered Oct 01 '22 00:10

Reuven