TextView随键盘弹出上移高度
2014-10-13 10:41
387 查看
很多时候我们都在为键盘遮挡了原本就不大的屏幕时而烦恼,特别是当用户处于编辑状态时,键盘下面的内容就看不见了,用户只能处于盲打状态了。现在有一种简单的解决办法,基本思路就是,添加通知。一直监听键盘事件,在键盘遮挡时,将编辑器上移键盘的高度,键盘消失时,编辑区回复原来位置,ok,来两段代码吧
- (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键盘输入 弹出指定高度 点击return收回虚拟键盘
- IOS UITableView中UITextField 弹出键盘时,界面上移
- Android实现键盘弹出界面上移的实现思路
- 当键盘弹出时,获取键盘高度
- 获取弹出键盘时间,,,与高度
- 监听键盘弹出和隐藏,得到键盘高度
- 视图随键盘的弹出整体上移UIKeyboardWillChangeFrameNotification
- 在android中键盘弹出和收起会改变window的高度,因此监听window的resize
- iOS开发:TextField随着键盘的弹出上移
- Android 中使 EditText 失去焦点,禁止弹出软键盘,及 弹出软键盘后 EditText 自动上移 不致被遮挡
- iOS键盘弹出的时候会挡住底部的控件,可以通过以下方法将整个UIView上移。
- iOS 键盘弹出视图上移
- 监听键盘弹出高度
- 键盘弹出时textfield也随着上移,键盘隐藏时,textfield回到原来的位置
- 点击输入框弹出键盘布局上移
- 完美解决EditText被软盘遮挡和键盘弹出布局不上移
- Android监听键盘是否弹出和获取软键盘的高度
- Android软键盘弹出时窗口上移的问题
- 软键盘的弹出隐藏监听及软键盘高度
- Xamarin: Moving the view on Keyboard show 弹出键盘时移动view高度