KeyBoardDidShow和KeyBoardDidHide使用通知,在Portrait中工作但在Landscape中有问题

时间:2011-11-17 08:48:26

标签: iphone ipad ios4 xcode4

我想使用我的应用程序纵向和横向模式,
我有一个问题:在我的一个处于纵向模式的屏幕中,我正在使用KeyboardDidShow和KeyBoardDidiHide的通知。
实现KeyboardDidshow的逻辑是

-(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;        

}
当我点击最后一个复选框按钮时,滚动将图像向上移动并显示键盘和&当键盘最小化时,屏幕返回正常位置。 这适用于Portarit模式,但在横向模式下,当我点击最后一个复选框按钮时,我无法看到屏幕

什么是解决方案?

有人可以帮助我吗

先谢谢你 微酸

1 个答案:

答案 0 :(得分:0)

您可以在运行时获取设备方向并相应地调整大小。

    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;

不要忘记在viewWillAppear和viewWillDisappear方法中添加它。否则

[[UIDevice currentDevice] orientation]将返回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];
}

试试这个并告诉我。