键盘隐藏UIView iOS

时间:2012-07-30 13:38:03

标签: objective-c ios5

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

我试图向上移动的视图位于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秒重新加载一次,以便可以使用任何新消息更新消息。

1 个答案:

答案 0 :(得分:1)

文本字段已经向上移动但在您离开视图时没有向下移动。当您再次访问该视图时,它会再次向上移动 - 然后离开屏幕。

我以前遇到过这个问题;通过保持布尔变量来指示文本字段是处于向上还是向下位置来解决它。检查变量以确保文本字段尚未启动,然后再将其移动。

我是怎么做到的。我在我的视图中使用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];
    }
}

<强> - 编辑 -

查看代码时,您似乎从帧的y坐标中减去以向下移动帧。 CGRect的iOS坐标系与普通坐标系不同 - y轴被翻转(这对于图形系统来说相对常见)。你会想要做与你正在做的相反的事情。