0

I have a UITextField embedded in a UIScrollView, and I want the text field to scroll up to be visible when the keyboard is active. I tried the top answer in How to make a UITextField move up when keyboard is present? but it seems that it does not work with auto-layout, so instead I took the code from the apple docs. This almost works, but its not scrolling the view until the user actually enters some data, rather than when the keyboard actually appears.

Here's what I'm doing - is there something obviously wrong?

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(keyboardWillShow:)
                                               name:UIKeyboardWillShowNotification object:nil];

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

}

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

  UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
  self.scrollView.contentInset = contentInsets;
  self.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, self.activeField.frame.origin) ) {
    [self.scrollView scrollRectToVisible:self.activeField.frame animated:YES];
  }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:0.3];
  UIEdgeInsets contentInsets = UIEdgeInsetsZero;
  self.scrollView.contentInset = contentInsets;
  self.scrollView.scrollIndicatorInsets = contentInsets;
  [UIView commitAnimations];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
  NSLog(@"textFieldDidEndEditing");
  self.activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
  NSLog(@"textFieldDidEndEditing");
  self.activeField = nil;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self registerForKeyboardNotifications];
}
Community
  • 1
  • 1
Michael Anderson
  • 61,385
  • 7
  • 119
  • 164

2 Answers2

0

Try the code below:

    -(void)keyboardWillShow:(id)sender
    {

            if ([orientation==@"Landscape"])
                [loginScrollView setContentSize:CGSizeMake(480, 620)];
            else
                [loginScrollView setContentSize:CGSizeMake(320, 670)];
    }


 and while editing the text use this 

- (void)textFieldDidBeginEditing:(UITextField *)textField {

     if ([orientation == @"Landscape"]) {
        if (textField == usernameTextField)
            [loginScrollView setContentOffset:CGPointMake(xValue, 200) animated:YES];
        if (textField == passwordTextField)
            [loginScrollView setContentOffset:CGPointMake(xValue, 220) animated:YES];
    }
    else{
        if (textField == usernameTextField)
            [loginScrollView setContentOffset:CGPointMake(xValue, 168) animated:YES];
        if (textField == passwordTextField)
            [loginScrollView setContentOffset:CGPointMake(xValue, 172) animated:YES];
    }
}
brettfazio
  • 906
  • 8
  • 23
ArunMak
  • 398
  • 2
  • 12
0

Try this,

ViewController.h

bool keyboardIsShown;
UITapGestureRecognizer *tapGesture;

ViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];

scrollView.contentSize =CGSizeMake(0, self.view.frame.size.height+50);

// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:self.view.window];
keyboardIsShown = NO;

}


- (void)viewDidUnload
 {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillShowNotification
                                              object:nil];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillHideNotification
                                              object:nil];
}

///// KEYBOARD

- (void) moveView:(float) yPos
 {
[scrollView setContentOffset:CGPointMake(0, yPos) animated:YES];
 }


- (void)keyboardWillHide:(NSNotification *)n
{
keyboardIsShown = NO;
[self.view removeGestureRecognizer:tapGesture];
[self moveView:0.0];
}

- (void)keyboardWillShow:(NSNotification *)n
{
  if (keyboardIsShown)
{
    return;
    }

keyboardIsShown = YES;

[self.view addGestureRecognizer:tapGesture];

    [self moveView:255.0];
}

// method to hide keyboard when user taps on a scrollview
 -(void)dismissKeyboard
  {
[self.view endEditing:YES];
  }
Ravindhiran
  • 4,974
  • 7
  • 46
  • 79