键盘隐藏UIView iOS

键盘隐藏UIView iOS

问题描述:

我知道这个问题已被问了几次。我明白如何在键盘出现时向上移动视图或文本框,但是我遇到了一个我似乎无法弄清楚的小故障。键盘隐藏UIView iOS

我试图向上移动的视图是一个UIViewController,它充当两个视图的容器。这个容器本身就是一个滑动视图控制器内的视图(类似于Facebook实现的那种)。

当您第一次加载视图时,文本字段向上移动,但如果您转到其他视图并返回,则键盘会导致视图消失。

这里是我使用的移动视图了代码:

- (void) animateTextField:(BOOL)up { 

    const int movementDistance = 350; // tweak as needed 
    const float movementDuration = 0; // tweak as needed 

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

    [UIView beginAnimations: @"anim" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 

    self.sendingToolbar.frame = CGRectOffset(self.sendingToolbar.frame, 0, movement); 
    self.messagesTable.frame = CGRectOffset(self.messagesTable.frame, 0, movement); 
    //[splitController moveViewUp:up]; 
    [UIView commitAnimations]; 

} 

- (void)registerForKeyboardNotifications 
{ 
    NSLog(@"was this method called"); 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification object:nil]; 

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

} 

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    [self animateTextField:YES]; 
} 
// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    [self animateTextField:NO]; 
} 

任何想法,为什么这种故障可能发生的?谢谢

在为布尔值添加建议以检查文本字段是否已经启动之后,视图会一直显示键盘时消失,而不仅仅是当您离开视图并返回时。下面是修改后的方法:

- (void) animateTextField:(BOOL)up { 

    const int movementDistance = 350; // tweak as needed 
    const float movementDuration = 0; // tweak as needed 

    int movement = movementDistance; 

    [UIView beginAnimations: @"anim" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 

    if(textFieldIsUp && (up == YES)) { 
     NSLog(@"Case 1"); 
     // Do nothing since text field is already up 
    } 
    else if(textFieldIsUp && (up == NO)) { 
     NSLog(@"Case 2"); 
     // Move the text field down 
     self.sendingToolbar.frame = CGRectOffset(self.sendingToolbar.frame, 0, -movement); 
     self.messagesTable.frame = CGRectOffset(self.messagesTable.frame, 0, -movement); 

     textFieldIsUp = NO; 
    } 
    else if((textFieldIsUp == NO) && (up == YES)) { 
     NSLog(@"Case 3"); 
     // Move the text field up 
     self.sendingToolbar.frame = CGRectOffset(self.sendingToolbar.frame, 0, movement); 
     self.messagesTable.frame = CGRectOffset(self.messagesTable.frame, 0, movement); 

     textFieldIsUp = YES; 
    } 
    else if((textFieldIsUp == NO) && (up == NO)) { 
     NSLog(@"Case 4"); 
     // Do nothing since the text field is already down 
    } 
    else { 
     NSLog(@"Default"); 
     // Default catch all case. Does nothing 
    } 

    [UIView commitAnimations]; 

} 

这里有一些关于我的设置一些细节:

视图控制器是应用程序消息收发中心。视图控制器包含两个子视图,左侧的子视图是用于选择对话的菜单,右侧的菜单是包含该对话内的消息的子视图。由于我想从下往上加载消息,因此表视图旋转了180度,单元格也旋转了180度,方向相反。此外,表格视图使用NSTimer每5秒重新加载一次,以便可以使用任何新消息更新消息。

+0

不用“NSTimer”,你应该看看键值观察。在后台使用计时器自动检查更新通常是不好的做法(相当低效)。 – Dustin 2012-07-30 15:07:23

+0

我会研究一下。这对于使用REST服务检查新消息是否合适?我认为包含消息的数组必须不断从服务器进行更新。我也想找到一种方法来更新消息表,而不必重绘整个表。如果我做了这个键值观察,我想这会起作用。 – 2012-07-30 15:28:12

+0

如果您使用的是服务器,您应该让用户决定何时更新他们的消息。从服务器中绘制消耗了不少数量的开销。我不知道如何在不重绘表格的情况下更新表格。 KVO用于查看对象的属性并在属性更改时调用某种方法,因此我不确定它是否会按照您的要求进行操作。如果你需要帮助,试着看看你可以用它做什么,并在SO上发布另一个问题。 – Dustin 2012-07-30 15:43:57

当您离开视图时,textfield已被移动但未移回。当你重新审视视图时,它会再次移动 - 并离开屏幕。

我以前有过这个问题;通过保持一个布尔变量来解决它,表明文本字段是处于向上还是向下的位置。检查变量以确保文本字段在您再次移动之前尚未启动。

我是如何做到的。我在我的视图中使用NSNumber属性来存储视图是否已被推高,以便其他视图可以传递是否已将视图向上或向下推送。一帧的

//In viewDidLoad 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil]; 

//Pushes the view up if one of the table forms is selected for editing 
- (void) keyboardDidShow:(NSNotification *)aNotification 
{ 
    if ([isRaised boolValue] == NO) 
    { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.25]; 
     self.view.center = CGPointMake(self.view.center.x, self.view.center.y-moveAmount); 
     [UIView commitAnimations]; 
     isRaised = [NSNumber numberWithBool:YES]; 
    } 
} 

//Push view back down 
- (void) keyboardDidHide:(NSNotification *)aNotification 
{ 
    if ([isRaised boolValue]) 
    { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.25]; 
     self.view.center = CGPointMake(self.view.center.x, self.view.center.y+moveAmount); 
     [UIView commitAnimations]; 
     isRaised = [NSNumber numberWithBool:NO]; 
    } 
} 

CNC中

你的代码看,似乎你从减去y坐标移动框架下来。 CGRect的iOS坐标系与正常坐标系不同 - y轴被翻转(这对于图形系统而言是相对常见的)。你会想做与你正在做的事情相反的事情。

+0

当我离开视图时,键盘变得隐藏。不应该称为keyboardWillBeHidden方法,因此animateTextField?我记录了该方法是否被调用,并且我注意到每次注册键盘事件时都会调用两次animateTextField方法。 – 2012-07-30 14:05:37

+0

只有在发生隐藏时仍处于相同视图中时,才会调用“keyboardWillBeHidden”通知。 – Dustin 2012-07-30 14:09:03

+0

我将为布尔值添加条件并让您知道发生了什么,感谢迄今为止的详细答案。 – 2012-07-30 14:14:47