15

[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];
    }
}
Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
thenetimp
  • 9,487
  • 5
  • 29
  • 42

4 Answers4

33

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

Works like a charm...

Reuven
  • 2,142
  • 3
  • 18
  • 30
  • 1
    +1 Easy as hell to implement and really good. I suggest combining it with the BSKeyboardControl to add navigation control in the textfields: http://www.cocoacontrols.com/controls/bskeyboardcontrols – Groot Mar 20 '13 at 11:13
  • @Reuven When using TPKeyboardAvoidingScrollview, Sometimes if we copy and paste in textview the view entirely goes up,.. is there any solution for that?.. – Maniganda saravanan Feb 21 '14 at 14:03
  • 1
    Does it work with `UIPresentationModeFormSheet` though? – fatuhoku May 08 '14 at 14:24
3

Have you ever added an observer for that specific notification? Make sure that in your loadView method you do this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];

Don't forget to unregister the observer on viewDidUnload method like this:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Let me know if that works out!

Mihai Fratu
  • 7,579
  • 2
  • 37
  • 63
  • I did add the observers, as I mentioned I can see the function is called when the keyboard shows, so I do know that the observer is working. Thanks though always good to double check things, – thenetimp Dec 21 '11 at 19:30
  • 2
    You need to update this post. viewDidUnload is no longer called. – Cameron Lowell Palmer Oct 20 '13 at 12:57
0

One simple solution is to add the extension UIViewController+Keyboard.swift to your project, with a single line

setupKeyboardNotifcationListenerForScrollView(scrollView)

it will auto resize automatically when keyboard appears. No need to subclass anything, just an extension! Its availble at GitHub SingleLineKeyboardResize

bogen
  • 9,954
  • 9
  • 50
  • 89
0

I've done this several times with a UITableView (which is just an extended UIScrollView). You can find the code in this answer.

Community
  • 1
  • 1
DBD
  • 23,075
  • 12
  • 60
  • 84