3

这是一个很菜鸟的问题。当UIKeyBoard显示时,我在底部有一个UIToolBar,它应该可以通过键盘动态地上下移动。我在UIKeyBoard通知的帮助下工作。我们正在讨论的视图已启用分割视图。当设备方向是横向时,两个视图都显示为列[希望是有道理的]。UIKeyBoard调整方向更改为横向

当显示按键板,我这样做

CGSize keyBoardSize = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size; 

CGRect toolbarFrame= [BottomToolBar frame]; 
toolbarFrame.origin.y -= keyBoardSize.height;  
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.3]; 
BottomToolBar .frame = viewFrame; 
[UIView commitAnimations]; 

当钥匙板被hiiden我这样做

toolbarFrame.origin.y += keyBoardSize.height; 

我的问题是设备取向变成横向,当键板是当可见底部工具栏消失了。我看到它迅速上升。我不知道如何解决这个问题。任何人都可以帮忙吗?另外,有没有办法让键盘跨越分割视图中的两个视图?

回答

10

我也有这个问题,所有我能想到的是关闭键盘和重新显示它(辞职,然后再次成为第一响应者)。但是这似乎很不令人满意。

请注意,您应该将屏幕坐标的矩形转换为视图的坐标。 (屏幕坐标不旋转。)

CGRect keyboardRect = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue]; 
keyboardRect = [[BottomToolBar superview] convertRect:keyboardRect fromView:nil]; 

更新:您必须UIKeyboardWillShowNotification注册,那么你的操作将被调用的时候界面旋转:)

另见: https://devforums.apple.com/message/181482#181482

+0

如果您试图将工具栏保留在键盘框架的上方并显示通话状态栏,则此转换至关重要。你可以使用键盘命令CMD + Y – brynbodayle 2013-09-29 02:30:53

6
- (void)viewDidLoad { 
    [super viewDidLoad]; 

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

- (void)keyboardWillShow:(NSNotification *)aNotification 
{ 
    CGRect keyboardBounds; 
    [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds]; 

    CGFloat keyboardHeight; 
    switch ([UIApplication sharedApplication].statusBarOrientation) { 
     case UIInterfaceOrientationPortrait: 
     case UIInterfaceOrientationPortraitUpsideDown: 
      keyboardHeight = keyboardBounds.size.height; 
      break; 
     case UIInterfaceOrientationLandscapeLeft: 
     case UIInterfaceOrientationLandscapeRight: 
      keyboardHeight = keyboardBounds.size.width; 
      break; 
    } 

    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 

    CGRect rect = table.frame; 
    rect.size.height -= keyboardHeight; 
    [UIView beginAnimations:@"ResizeForKeyboardShow" context:nil]; 
    [UIView setAnimationDuration:animationDuration]; 
    table.frame = rect; 
    [UIView commitAnimations]; 
} 

- (void)keyboardWillHide:(NSNotification *)aNotification 
{ 
    CGRect keyboardBounds; 
    [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds]; 

    CGFloat keyboardHeight; 
    switch ([UIApplication sharedApplication].statusBarOrientation) { 
     case UIInterfaceOrientationPortrait: 
     case UIInterfaceOrientationPortraitUpsideDown: 
      keyboardHeight = keyboardBounds.size.height; 
      break; 
     case UIInterfaceOrientationLandscapeLeft: 
     case UIInterfaceOrientationLandscapeRight: 
      keyboardHeight = keyboardBounds.size.width; 
      break; 
    } 

    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 

    CGRect rectTable = table.frame; 
    rectTable.size.height += keyboardHeight; 
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
    [UIView setAnimationDuration:animationDuration]; 
    table.frame = rectTable; 
    [UIView commitAnimations]; 
} 
+1

在模拟器中测试这个不要忘记[[NSNotificationCenter defaultCenter] removeObserver:self]; on viewDidUnload和dealloc – cynistersix 2012-05-10 01:09:29

+0

仅供参考:您可以使用方便的'UIDeviceOrientationIsPortrait'宏而不是那个大的'switch'和'case'怪物。 :) – 2012-10-12 08:25:32

0

这些答案很好,除了外接键盘。当硬件键盘出现时,它看起来像在消息中传递的键盘框架的“高度”属性仍然与使用虚拟键盘时的相同,这意味着该代码从视图框架中减去高度,现有的键盘(创造一个尴尬的空间)。

我发现的最佳解决方案是注意到键盘框架的“y”属性被指定在屏幕的底部(意味着虚拟键盘实际存在,就在屏幕之外)。

4

UITextFields有一个inputAccessoryView属性,允许在UIKeyboard上方添加UIToolBar。

+0

是的,但OP希望工具栏在隐藏键盘时在屏幕底部可见。 – ma11hew28 2012-09-02 20:51:35

+0

没问题,只需在底部设置工具栏作为inputAccessoryView,就完成了。 – nverinaud 2013-05-03 07:13:22

0

的解决方案是基于块的动画设置选项UIViewAnimationOptionBeginFromCurrentState,或

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
BottomToolBar.frame = viewFrame; 
[UIView commitAnimations]; 

你的情况。然后,当从纵向改变方向到风景时,视图不会突然移动。

2
- (void)viewDidLoad { // Or somewhere else 
    [super viewDidLoad]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbDidChange:) UIKeyboardDidChangeFrameNotification object:nil]; 
} 

- (void)kbDidChange:(NSNotification *)notification { 
    NSDictionary* keyboardInfo = [notification userInfo]; 
    CGRect keyboardFrame = [[keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    keyboardFrame = [[messageBar superview] convertRect:keyboardFrame fromView:nil]; 

    CGRect ol = messageBar.frame; // messageBar is your UIToolbar.. 
    ol.origin.y = keyboardFrame.origin.y-44; 
    messageBar.frame = ol; 
} 
+0

您可以考虑解释此代码,以及它为什么回答手头的问题? – 2012-09-21 21:39:11

相关问题