Search code examples
iphoneipadios4xcode4

KeyBoardDidShow and KeyBoardDidHide Using Notification's, Working in Portrait but having an issue in Landscape


I want to use my app both the portrait and Landscape Mode ,
I have a problem : In one of my screen in Portrait mode i am using Notifications for KeyboardDidShow and KeyBoardDidiHide .
The logic for implementing the KeyboardDidshow is

-(void)keyboardDidShow: (NSNotification*) notif{          
if (keyboardVisible) {        
    return;  
}  
NSDictionary* info = [notif userInfo];  
NSValue *avalue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey]; 
CGSize KeyboardSize = [avalue CGRectValue].size;  
CGRect viewFrame = scrollview.frame;  
viewFrame.size.height -= KeyboardSize.height;  
scrollview.frame = viewFrame;  
CGRect textViewdRect = [customTextViewTwo frame];  
[scrollview scrollRectToVisible: textViewdRect animated:YES];  
keyboardVisible = YES;  

}

-(void)keyboardDidHide: (NSNotification*) notif{  
if (!keyboardVisible) {
    return;  
}  
NSDictionary *info = [notif userInfo];  
NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];  
CGSize KeyboardSize = [avalue CGRectValue].size;   
CGRect viewFrame = scrollview.frame;
viewFrame.size.height += KeyboardSize.height;  
scrollview.frame = viewFrame;    
keyboardVisible = NO;        

}
When i click in the last checkbox button the scroll take the images up and keyboard is shown & when minimized the Keyboard the screen returns to the normal position. This is working fine for Portarit mode but in landscape Mode i am unable to see the screen when i am clicking on the last checkbox button

What can be the Solution?

Can Somebody help me out for this

Thanks In Advance
Sourish


Solution

  • You can get the device orientation at runtime and adjust the size accordingly.

        CGFloat _keyboardHeight;
    if ([[UIDevice currentDevice] orientation] ==  UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] ==  UIDeviceOrientationLandscapeRight) {
        _keyboardHeight = KeyboardSize.width;
    }
    else {
        _keyboardHeight = KeyboardSize.height;
    }
    
    CGRect viewFrame=_scrollView.frame;
    viewFrame.size.height += _keyboardHeight;
    _scrollView.frame=viewFrame;
    

    Don't forget to add this in viewWillAppear and viewWillDisappear methods.Otherwise

    [[UIDevice currentDevice] orientation] will return UIDeviceOrientationUnknown

    -(void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        //generating device orientation notifications
        [[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];
    }
    
    -(void)viewWillDisappear:(BOOL)animated {
        //stop generating device orientation notifications
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    }
    

    Try this and let me know.