在textView中输入时避免键盘的遮挡
来源:互联网 发布:java file 文件大小 编辑:程序博客网 时间:2024/05/29 14:06
很多时候我们都在为键盘遮挡了原本就不大的屏幕时而烦恼,特别是当用户处于编辑状态时,键盘下面的内容就看不见了,用户只能处于盲打状态了。现在有一种简单的解决办法,基本思路就是,添加通知。一直监听键盘事件,在键盘遮挡时,将编辑器上移键盘的高度,键盘消失时,编辑区回复原来位置,ok,来两段代码吧
[cpp]
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textView=[[UITextView alloc]initWithFrame:self.view.frame];
self.textView.text=@"请输入文字";
[self.view addSubview:self.textView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
//注册通知,监听键盘出现
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(handleKeyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
//注册通知,监听键盘消失事件
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(handleKeyboardDidHidden)
name:UIKeyboardDidHideNotification
object:nil];
[super viewWillAppear:YES];
}
//监听事件
- (void)handleKeyboardDidShow:(NSNotification*)paramNotification
{
//获取键盘高度
NSValue *keyboardRectAsObject=[[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect;
[keyboardRectAsObject getValue:&keyboardRect];
self.textView.contentInset=UIEdgeInsetsMake(0, 0,keyboardRect.size.height, 0);
}
- (void)handleKeyboardDidHidden
{
self.textView.contentInset=UIEdgeInsetsZero;
}
- (void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textView=[[UITextView alloc]initWithFrame:self.view.frame];
self.textView.text=@"请输入文字";
[self.view addSubview:self.textView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
//注册通知,监听键盘出现
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(handleKeyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
//注册通知,监听键盘消失事件
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(handleKeyboardDidHidden)
name:UIKeyboardDidHideNotification
object:nil];
[super viewWillAppear:YES];
}
//监听事件
- (void)handleKeyboardDidShow:(NSNotification*)paramNotification
{
//获取键盘高度
NSValue *keyboardRectAsObject=[[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect;
[keyboardRectAsObject getValue:&keyboardRect];
self.textView.contentInset=UIEdgeInsetsMake(0, 0,keyboardRect.size.height, 0);
}
- (void)handleKeyboardDidHidden
{
self.textView.contentInset=UIEdgeInsetsZero;
}
- (void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- 在textView中输入时避免键盘的遮挡
- 在textView中输入时避免键盘的遮挡
- 在textView中输入时避免键盘的遮挡
- 在textView中输入时避免键盘的遮挡
- 在ios中避免键盘遮挡了输入框的处理
- 避免键盘弹出时遮挡输入框
- TableView的Cell中有Textfield、TextView时放置键盘遮挡输入框
- iOS输入框怎样避免键盘的遮挡
- 在移动web开发中,手机横屏时,输入框输入时被系统键盘遮挡!
- Swift 解决IOS开发中TableView中编辑UITextField时键盘遮挡输入框的情况
- iOS——获得键盘的高度 NSNotificationCenter 避免遮挡输入框
- iOS输入框自适应键盘高度,避免遮挡
- textView键盘遮挡问题
- textView、textField键盘遮挡
- [ios]TableView的Cell中有Textfield时放置键盘遮挡输入框
- iOS开发 键盘遮挡输入框(TextView、TextField)问题
- React中键盘遮挡输入框
- iOS_开发经验总结:键盘遮挡输入框时的解决方法
- int String互转
- POJ 2728 Desert King(最优比例生成树)
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- IAAS云计算产品畅想-云主机产品陷阱
- Linux平台下Qt应用程序开发之Hello world
- 在textView中输入时避免键盘的遮挡
- 深入理解OpenERP的工作流(Workflow)
- Notepad++ 编译器常用快捷键
- jquery.lazyload 实现图片延迟加载jquery插件
- 【转载】更改MDI窗体背景颜色
- android避免弹出软键盘遮盖listview
- 50个最新Web开发与设计资源
- ORA-01940无法删除当前已连接用户
- RTLabel 可以让label显示某段文字带下划线