IOS——UITextField被键盘遮蔽解决方案
来源:互联网 发布:淘宝上真正的原单店 编辑:程序博客网 时间:2024/05/22 19:26
本人在做ios开发的时候碰到一个老生常谈的问题,UITextField被键盘遮蔽问题,网上搜索了一些资料,找到了两种前辈写的方案,方案一:http://blog.csdn.net/springsky_/article/details/7941858,在ios5之前适用,但是在5之后盘布局变了,尤其是中文输入时,中文汉字选择框就固定在键盘上方,于是有前辈出了第二种方案:http://www.apkbus.com/home.php?mod=space&uid=107838&do=blog&id=44715。第二种方案是直接把整个view向上移动键盘的宽度,有些场景显得不是很恰当,本人在两位的基础上做了些许改动,具体步骤如下:
本人定义了一个基本的ViewController——BaseViewController。
.h文件内容如下:
#import <UIKit/UIKit.h>
@interface BaseViewController :UIViewController<UITextFieldDelegate>{
UITextField *_checkText; //用来标识哪一个UITextField被点击
@property(nonatomic) UITextField*checkText;
-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeightwithDuration:(NSTimeInterval)_NSTimeInterval;
@end
.m文件内容如下:
#import "BaseViewController.h"
@implementation BaseViewController
-(void)viewDidLoad{
[super viewDidLoad];
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];
// 键盘高度变化通知,ios5.0新增的
#ifdef __IPHONE_5_0
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 5.0) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillChangeFrameNotification object:nil];
}
#endif
}
#pragma mark 开始编辑UITextField,本人试过这个方法在keyboardWillShow之前被调用
-(void)textFieldDidBeginEditing:(UITextField*)textField{
_checkText = textField;//设置被点击的对象
}
#pragma mark -键盘弹出时调用的方法
#pragma mark Responding to keyboard events
- (void)keyboardWillShow:(NSNotification*)notification {
if (nil == _checkText) {
return;
}
/*
Reduce the size of the text view so that it's not obscured by thekeyboard.
Animate the resize so that it's in sync with the appearance of thekeyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin inself's view's coordinate system. The bottom of the text view's frame shouldalign with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
CGRect textFrame = _checkText.frame;//当前UITextField的位置
float textY = textFrame.origin.y + textFrame.size.height;//得到UITextField下边框距离顶部的高度
float bottomY = self.view.frame.size.height - textY;//得到下边框到底部的距离
if(bottomY >=keyboardRect.size.height ){//键盘默认高度,如果大于此高度,则直接返回
return;
}
float moveY = keyboardRect.size.height - bottomY;
// Animate the resize of the text view's frame in sync with the keyboard'sappearance.
[self moveInputBarWithKeyboardHeight:moveYwithDuration:animationDuration];
}
//键盘被隐藏的时候调用的方法
(void)keyboardWillHide:(NSNotification*)notification {
NSDictionary* userInfo = [notification userInfo];
/*
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of thekeyboard.
*/
NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[self moveInputBarWithKeyboardHeight:0.0withDuration:animationDuration];
}
#pragma mark 移动view
-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeightwithDuration:(NSTimeInterval)_NSTimeInterval{
CGRect rect = self.view.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:_NSTimeInterval];
rect.origin.y = -_CGRectHeight;//view往上移动
self.view.frame = rect;
[UIView commitAnimations];
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];//在视图控制器消除时,移除键盘事件的通知
}
希望对大家有帮助!
转自:http://blog.csdn.net/windkisshao/article/details/21398521
- IOS——UITextField被键盘遮蔽解决方案
- IOS——UITextField被键盘遮蔽解决方案
- UITextField与键盘——IOS
- iOS菜鸟学习——UITextField return键隐藏键盘
- IOS——UITextField自动适应键盘弹出
- iOS开发中防止键盘挡住UITextField解决方案
- iOS开发中防止键盘挡住UITextField解决方案
- iOS开发中防止键盘挡住UITextField解决方案
- ios学习--iphone 防止键盘挡住UITextField解决方案
- iOS 键盘挡住UITextField
- UITextField滑动防止被键盘挡住 终极解决方案
- UITextField滑动防止被键盘挡住 终极解决方案
- iOS——UITextField
- iOS—UITextField详解
- iOS中UITextField隐藏键盘
- iOS 使用UITextField隐藏键盘
- ios键盘遮挡UITextField问题
- iOS 使用UITextField隐藏键盘
- Windows 7程序开发系列之二(JumpList篇1 - User Task)
- 设置linux终端字符颜色
- [Erlang]erlang与php的通信
- Matplot中文乱码完美解决方案
- Android Animation动画(很详细)
- IOS——UITextField被键盘遮蔽解决方案
- c++学习二
- Android-IntentService
- Windows 7程序开发系列之二(JumpList篇2 - Destination)
- Linux应用程序学习之制作静态函数库
- 记录一个JS异常Uncaught NotFoundError
- Android-BoundService
- iOS 获取系统相册全部照片以及照片信息
- IOS银联支付的流程