keyboard hides textfield for different orientation in ipad

In my iPad application, I have few textView and textField's. When I click on textField, the keyboard covers the textField. So I am implementing the below code to move the textview up. But on rotation to portraitUpsideDown its not working fine. It slides the screen down in opposite direction. So how do I solve this problem??

-(void) animateTextField: (UITextView *) textField up: (BOOL) up
{
    int txtPosition = (textField.frame.origin.y - 540);
    const int movementDistance = (txtPosition < 0 ? 0 : txtPosition); // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

-(void)textViewDidBeginEditing:(UITextView *)textField
{
    [self animateTextField: textField up: YES];
}

-(void)textViewDidEndEditing:(UITextView *)textField
{
    [self animateTextField: textField up: NO];
}

-(BOOL)textFieldShouldReturn:(UITextView *)theTextField
{
    [theTextField resignFirstResponder];
    return YES;
}

  • Can not include .ttf font into project
  • Edge Detection of Image in iPhone Using Image Magick
  • GPX File does not load iOS Xcode
  • SWIFT: Why I can't get the current URL loaded in UIWebView?
  • Storyboards and nibs compatible?
  • loadView: functions in UIView iOS
  • 4 Solutions Collect From Internet About “keyboard hides textfield for different orientation in ipad”

    This solution is for iPhone, but it considers both orientations.

    You can adapt a bit and voilá:

    http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html

    Crazy,

    Just add another function:

    - (void) animateTextView: (UITextView*) textView up: (BOOL) up
    {
        const int movementDistance = 80; // tweak as needed
        const float movementDuration = 0.3f; // tweak as needed
    
        int movement = (up ? -movementDistance : movementDistance);
    
        [UIView beginAnimations: @"anim" context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
        [UIView commitAnimations];
    }
    

    Then call it like:

    - (void)textViewDidBeginEditing:(UITextView *)textView {
        [self animateTextView: textView up: YES];
    }
    
    - (void)textViewDidEndEditing:(UITextView *)textView {
        [self animateTextView: textView up: NO];
    }
    

    If your method like this.

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    

    Try this. I don’t know exactly. But I am trying to help you. May be x and y coordinate cannot be changed in any orientation. so try this.

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {
         if(interfaceOrientation=UIInterfaceOrienationPotraitUpsideDown){
              //Declare txtPos globally...
              txtPos=(textField.frame.origin.y + 540);
          }
         if(interfaceOrientation=UIInterfaceOrienationPotrait)
          {
              txtPos=(textField.frame.origin.y - 540);
          }
        return(YES);
      }
    

    in animate method.
    assign textPos to txtPosition variable..

    You should use keyboard will show and will hide notification to capture keyboard event and adjust your view accordingly.

    - (void)dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
    
    }
    
    - (void)keyboardWillShow:(NSNotification *)notification {
        CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame);
        CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
        UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
        UIViewAnimationOptions animationOption = animationCurve << 16;
    
        [UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{
            // adjust height using keyboardHeight
        } completion:^(BOOL finished) {
    
        }];
    }
    
    - (void)keyboardWillHide:(NSNotification *)notification {
        CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame);
        CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
        UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
        UIViewAnimationOptions animationOption = animationCurve << 16;
    
        [UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{
            // adjust height using keyboardHeight
        } completion:^(BOOL finished) {
    
        }];
    }
    

    This blog post explains this in detail

    http://charlie.cu.cc/2015/10/solution-to-the-ios-software-keyboard-cover-part-of-the-ui/