本人在做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];//在视图控制器消除时,移除键盘事件的通知
}
希望对大家有帮助!