Search code examples
iphoneipaduitextfielduiinterfaceorientationorientation-changes

Key board hides textField when Orientation Changes?


I have two textfields in a subview as viewLogin. when try to enter text key board is hiding them. I solved that problem by moving the viewLogin. Code is...

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

    UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

        viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/9, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
    else {
        viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
}

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

    UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
        viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/2.5, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
    else {
        viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
}

Problem here i facing is if i first change the orientation to Landscape and click on textField its working fine but if i first click on textField in portrait mode and then changed to Landscape left/right its not working..

can some help me to solve this....

Thanks in advance


Solution

  • This is what Apple uses (take a look at the KeyboardAccessory example): First, register for keyboard notifications in viewDidLoad

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

    and make sure you unregister in viewDidUnload

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    

    then add these methods to your implementation:

    - (void)keyboardWillShow:(NSNotification *)notification 
    {
        NSDictionary *userInfo = [notification userInfo];
    
        NSValue *keyboardEndFrameValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
        CGRect keyboardRect = [keyboardEndFrameValue CGRectValue];
        keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
    
        CGFloat keyboardTop = keyboardRect.origin.y;
        CGRect newTextViewFrame = self.view.bounds;
        newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
    
        NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
        NSTimeInterval animationDuration;
        [animationDurationValue getValue:&animationDuration];
    
        [UIView animateWithDuration:animationDuration
                         animations:^{
                             self.textView.frame = newTextViewFrame;
                         }];
    }
    
    
    - (void)keyboardWillHide:(NSNotification *)notification 
    {
        NSDictionary *userInfo = [notification userInfo];
    
        NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
        NSTimeInterval animationDuration;
        [animationDurationValue getValue:&animationDuration];
    
        [UIView animateWithDuration:animationDuration
                         animations:^{
                             self.textView.frame = self.view.bounds;
                         }];
    }
    

    I'm using it and I do not have any problems with orientation changes.