Question

I have spent about 2 days on this issue going through several stack overflow/online posts and can't seem to find an answer.

I have 6 UITextFields. The bottom two hide when the keyboard appears. However, when I start typing:

On a 4" screen (iPhone 5): The textfields scroll up and hide behind the navigation bar.

On a 3.5" screen (iPhone 3GS): The textfields scroll up right below the navigation bar, which is where it should be.

When the user clicks on the UITextFields, I want the UITextFields to scroll up and end up right before navigation bar, RIGHT AWAY. This way the field appears and ready for input and is not hidden waiting for the user to begin typing before scrolling up.

I'm not sure if this is relevant, but I have UIControllerView and inside, it has a containerView. The containerView does not cover the entire screen and starts at X:68 Y:7 Width: 237 Height: 351. Inside the ContainerView I have the UIScrollView with UITextFields. The containerView also has its own NavigationBar. On the iPhone 5 (4" Screen), when scrolled up, the TextFields are hiding behind the NavigationBar inside the containerView. On the iPhone 3GS (3.5" screen), UITextfield appear right below the NavigationBar, as they should.

Here is the code which I followed from Apple Docs:

.h 
@interface ContactViewController : UIViewController <UITextFieldDelegate>
{
    UITextField *activeField;
}

.m
- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0.0);
    pageScrollView.contentInset = contentInsets;
    pageScrollView.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);
    [pageScrollView setContentOffset:scrollPoint animated:YES];
    }
}


// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    pageScrollView.contentInset = contentInsets;
    pageScrollView.scrollIndicatorInsets = contentInsets;
}

I got the code from Apple Doc's: http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement

Was it helpful?

Solution

After days of struggling with this and contemplating with which route to take, I decided to turn my UIViewController into a UITableViewController that provides scroll functionailty built in. I made this decision primarily because in the future I'd like Localize my app in a few other langages and instaed of having to manually reinvent the wheel for each language, I thought it would be much easier to settle for a customized TableView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top