首页 > 代码库 > 豪哥学开发之聊天窗口中文输入键盘位置改变

豪哥学开发之聊天窗口中文输入键盘位置改变

在viewdidiload方法中除了

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    

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

这两个常用键盘方法外,还需要添加一个方法,这样当键盘切换到中文输入时使用,因为输入框下面会多一行中文选择,

#ifdef __IPHONE_5_0

    float version = [[[UIDevice currentDevice] systemVersion] floatValue];

    if (version >= 5.0) {

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardFrameDidChange:) name:UIKeyboardDidChangeFrameNotification object:nil];

    }

#endif

 上面这个就是需要调用的方法,keyBoardFrameDidChange这个方法名可以自己写一个方法,也可以用keyBoardWillShow,多写一个方法是为了让以后接手的人能够看懂我们这个方法是干什么的,

下面就是三种方法的实现:(一些被注释的代码我没有删掉,是分析尝试的过程)

-(void)keyBoardFrameDidChange:(NSNotification *)note

{

//    NSDictionary *userInfo = [notification userInfo];

//    NSValue *aValue = http://www.mamicode.com/[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

//    CGRect keyboardRect = [aValue CGRectValue];

//    NSValue *animationDurationValue = http://www.mamicode.com/[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

//    NSTimeInterval animationDuration;

//    [animationDurationValue getValue:&animationDuration];

    

    CGSize size = [[UIScreen mainScreen] bounds].size;//获取屏幕的尺寸;

    CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];//得到键盘的高度;

    CGFloat ty = size.height - rect.size.height;//

    

    

    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{

        

        //frame.size.height -= rect.size.height;

       

        if (rect.origin.y != size.height) {

            CGRect frame = self.tableChat.frame;

            frame.size.height = size.height - rect.size.height - 40;//屏幕高度减去键盘高度减去输入框高度,就是新的table的高度,再赋给table,

              [self.bottomBar setFrame:CGRectMake(0, ty-40, 320, 40)];

             [self.tableChat setFrame:frame];

        }

        else{

            CGRect frame = self.tableChat.frame;

            frame.size.height = size.height - 40;

            [self.bottomBar setFrame:CGRectMake(0, size.height-40, ScreenWidth, 40)];

        }

        

//        if (self.bottomBar.frame.origin.y == size.height - 40) {

//            [self.bottomBar setFrame:CGRectMake(0, ty-40, 320, 40)];

//        }

//        else{

//            [self.bottomBar setFrame:CGRectMake(0, size.height - 40, ScreenWidth, 40)];

//        }

       

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:arrayMessagesFrame.count - 1 inSection:0];

        if (arrayMessagesFrame.count== 0) {

            return ;

        }

        else

        {

            [self.tableChat scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

        }

    }];

 

}

 

- (void)keyBoardWillShow:(NSNotification *)note{

    

    CGSize size = [[UIScreen mainScreen] bounds].size;

    CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGFloat ty = size.height - rect.size.height;

   

    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{

        CGRect frame = self.tableChat.frame;

        //frame.size.height -= rect.size.height;

        frame.size.height = size.height - rect.size.height - 40;

        [self.tableChat setFrame:frame];

        

        [self.bottomBar setFrame:CGRectMake(0, ty-40, 320, 40)];

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:arrayMessagesFrame.count - 1 inSection:0];

        if (arrayMessagesFrame.count== 0) {

            return ;

        }

        else

        {

            [self.tableChat scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

        }

    }];

}

#pragma mark 键盘即将退出

- (void)keyBoardWillHide:(NSNotification *)note{

    

    CGSize size = [[UIScreen mainScreen] bounds].size;

    CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGFloat ty = size.height - rect.size.height;

    

    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{

        CGRect frame = self.tableChat.frame;

        frame.size.height += rect.size.height - 60;

       

        [self.tableChat setFrame:frame];

        

        [self.bottomBar setFrame:CGRectMake(0, size.height-40, 320, 40)];

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:arrayMessagesFrame.count - 1 inSection:0];

        if (arrayMessagesFrame.count== 0) {

            return ;

        }

        else

        {

            [self.tableChat scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

        }

    }];

    

}

#pragma mark - 文本框代理方法

#pragma mark 点击textField键盘的回车按钮

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    

    NSString *content = textField.text;

    if ([content isEqualToString:@""]) {

        return NO;

    }

 

    [self addMessageWithContent:content type:MessageTypeMe];

    

    NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];

    NSString* strUid = [pref stringForKey:@"uid"];

    

   //NSString *strFrom = @"andy";

   // NSString *strTo   = @"andy";

    NSString *strMsg = [NSString stringWithFormat:@"{\"type\":\"chat\",\"from\":\"%@\",\"to\":\"%@\",\"msg\":\"%@\"}",strUid,self.msg.strContact,content];

    

    NSLog(@"input message:%@",strMsg);

 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"AppSendMessage" object:strMsg];

    

    [self.tableChat reloadData];

    

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:arrayMessagesFrame.count - 1 inSection:0];

    [self.tableChat scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

 

    self.messageField.text = nil;

    

    return YES;

}