简体   繁体   中英

UITextField moving when keyboard will block it

I am trying to make it so that when you click the email field and the keyboard pops up it would move the view up. But right now with this code it moves the view up no matter what textfield I click. Also I cant get the keyboard to dismiss. Not sure how to set this code to only scroll to the active field?

code:

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification 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, 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, self.emailField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, self.emailField.frame.origin.y-kbSize.height);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}

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

I have a view that looks like this (it is on a scrollview)

视图

For this you can ignore the keyboard show/hide notifications and just use the UITextFieldDelegate protocol:

– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if([textField isEqual:self.emailTextField]){
        // scroll up
    }
    return true;
}

– (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if([textField isEqual:self.emailTextField]){
        // scroll back to start
    }
    return true;
}
– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if([textField isEqual:YourTextfieldName])
{

}
return true;      // return true is needed.
}

 – (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
  {
   if([textField isEqual:YourTextfieldName])
      {

      }
   return true;    //return true is needed.
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM