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

在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 = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

//    CGRect keyboardRect = [aValue CGRectValue];

//    NSValue *animationDurationValue = [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;

}

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

时间: 2024-07-31 08:23:58

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

豪哥学ios开发之登录界面

首先在ViewController.h文件中: #import <UIKit/UIKit.h> #import "SRWebSocket.h" //继承SRWebSocket.h,作为登录服务器 @interface ViewController : UIViewController<UITextFieldDelegate,SRWebSocketDelegate> //- (IBAction)SaveLogin:(id)sender; //与SB联系账号输入,密

跟马哥学linux (lesson 6)linux包管理程序rpm & yum

一.rpm 1.什么是RPM RPM 是 Red Hat Package Manager 的缩写,本意是Red Hat 软件包管理,顾名思义是Red Hat 贡献出来的软件包管理:在Fedora .Redhat.Mandriva.SuSE.YellowDog等主流发行版本,以及在这些版本基础上二次开发出来的发行版采用. 2.rpm语法格式 1)查询,验证           rpm {-q|--query} [select-options] [query-options] rpm {-V|--v

CSDN学院“跟着龙哥学JavaWeb”的笔记

笔记内容都来源于""跟着龙哥学JavaWeb"课程中的讲解. 其实很多年前有接触,只是重新看看有什么更值得学习的. 既想搭建个服务器,又想做个APP端,那还是工作挺多的. 1 Web概述 C/S VB,VC++,Java,C# 特点: 在服务器端主要就是一个数据库,把所有业务逻辑以及界面的渲染操作交给客户端完成. 优点:较安全,用户节目很丰富,用户体验不错等. 缺点:每次升级都需要重新安装,针对于不同的操作系统开发,可移植性差. B/S JSP,ASP,PHP,基于浏览器访问

运维的我要学开发--Flask(1)

Flask默认使用的是Jinja2的模板引擎,下面将会介绍下Flask提供给模板的一些方法. #-*- coding: utf-8 -*- #导入一些函数 from flask import Flask from flask import render_template,g #创建一个app app = Flask(__name__) #创建一个装饰器 @app.route("/") @app.route("/index") def index(): string=

运维的我要学开发--Python(3)

本文均来自Python Cookbook,本博文励志将Cookbook中的string部位,文件,面向对象编程部分完成,本系列博文陆续还会介绍flask框架,每篇博文都以一个个小例子展示. #-*- coding: utf-8 -*- ''' 检查一个文本是字符串还是二进制 思想:如果字符串中包含了空值或者其中含有超过30%的字符 的高位被置1或是奇怪的控制码,我们就人为这段数据是二进制数据 ''' from __future__ import division import string #所

运维的我要学开发--Python(4)

本文均来自Python Cookbook,本博文励志将Cookbook中的string部位,文件,面向对象编程部分完成,本系列博文陆续还会介绍flask框架,每篇博文都以一个个小例子展示. #-*- coding: utf-8 -*- ''' 文件相关的操作: read() open() 两个参数: 第一个:文件路径 第二个:打开文件的模式 常用的文件打开模式: r:文本模式读取 rb:二进制模式读取 w:创建文件并文本模式 wb:创建文件,二进制模式写 rU:通用换行符的文本模式读取文件(独立

跟鸟哥学Linux之——学习总结

整个下来看<鸟哥的Linux私房菜--基础篇>学习了很多的知识,从基本的Linux初步认识,到一些文件目录的认识和操作,一些基本命令的使用,最后初步认识了Linux中进程的操作和控制方法.现阶段对Linux基础的认识和学习就这些,下边是学习下来的博客总结: 1.跟鸟哥学Linux之--Linux规划!! 2.跟鸟哥学Linux之--从磁盘分区到文件系统 3.跟鸟哥学Linux之--浅谈文件权限 4.跟鸟哥学Linux之--对文件与目录操作命令汇总 5.跟鸟哥学Linux之--bash shel

iOS开发-url包含中文报错解决办法

经常, 我们用通过这样的方法调用API. NSString* urlString = [NSString stringWithFormat:@"http://api.douban.com/v2/movie/search?q=%@", content]; NSURL *url = [NSURL URLWithString:urlString]; testRequest = [ASIHTTPRequest requestWithURL:url]; [testRequest setDeleg

跟马哥学linux (lesson 8)awk & sed功能总结

一.awk 1.什么是awk awk 是一种程序语言. 它具有一般程序语言常见的功能.  因awk语言具有某些特点, 如 : 使用直译器(Interpreter)不需先行编译; 变量无类型之分(Typeless), 可使用文字当数组的下标(Associative Array)...等特色. 因此, 使用awk撰写程序比起使用其它语言更简洁便利且节省时间. awk还具有一些内建功能, 使得awk善于处理具数据行(Record), 字段(Field)型态的资料; 此外, awk内建有pipe的功能,