本文介绍了键盘隐藏UIView iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

I know this question has been asked a few times. I understand how to move the view up or a textfield up when the keyboard is presented, however I am running into a glitch that I cannot seem to figure out.

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

The view I am trying to move up is inside a UIViewController that acts as a container for two views. This container is itself a view inside a sliding view controller (kind of like the one Facebook implements).

第一次加载视图时文本字段向上移动但是如果你转到另一个视图并返回,键盘会导致视图消失。

The text field moves up fine when you first load the view however if you go to a different view and come back, the keyboard causes the view to disappear.

以下是我用来移动视图的代码:

Here is the code I am using to move the view up:

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

为什么会出现这个小故障?谢谢

Any ideas why this glitch might be happening? Thanks

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

After adding the suggestion for a boolean to check if the text field is already up the view disappears all the time when the keyboard is shown and not just when you leave the view and come back. Here is the revised method:

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

}

以下是有关我的设置的更多详细信息:

Here are some more details about my setup:

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

The view controller is a messaging center for the app. The view controller contains two subviews the subview on the left side is a menu for picking the conversation and the right menu is a subview with the messages within that conversation. Since I want the messages to load from the bottom up, the table view is rotated 180 degrees and the cells are also rotated 180 degrees the opposite direction. Also the table view reloads every 5 seconds using an NSTimer so that the messages can be updated with any new messages.

推荐答案

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

The textfield has already been moved up but didn't move back down when you left the view. When you revisit the view it gets moved up again - and off the screen.

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

I've had this problem before; solved it by keeping a boolean variable the indicated whether the textfield was in the up or down position. Check the variable to make sure the textfield isn't already up before you move it up again.

我是如何做到的。我在我的视图中使用 NSNumber 属性来存储视图是否已被推高,以便其他视图可以通信他们是否已向上或向下推动视图。

How I did it. I'm using an NSNumber property on my view to store whether the view has been pushed up or not so that other views can communicate whether they have pushed the view up or down.

//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];
    }
}

-EDIT -

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

Looking at your code it appears that you're subtracting from the y-coordinate of a frame to move frame down. The iOS coordinate system for CGRect is not the same as a normal coordinate system - the y-axis is flipped (this is relatively common for graphic systems). You're going to want to do the opposite of what you're doing.

这篇关于键盘隐藏UIView iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:52