.h代码
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITextFieldDelegate> { int i_offset; //偏移量 int i_textFieldY; //textField 的y 值 int i_textFieldHeight; //textField的高度 IBOutlet UITextField *uitf_textField1; IBOutlet UITextField *uitf_textField2; IBOutlet UITextField *uitf_textField3; } @end
.m代码
#import "ViewController.h" #define StautsBarHeight 20.0f @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. uitf_textField1.delegate = self; uitf_textField2.delegate = self; uitf_textField3.delegate = self; i_offset = 0; //默认偏移量为0 i_textFieldY = 0; i_textFieldHeight = 0; //注册键盘监听消息 [self registerKeyBoardNotification]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } #pragma mark- UITextFieldDelegateMethod - (void)textFieldDidBeginEditing:(UITextField *)textField { NSLog(@"进入textFieldDidBeginEditing"); i_textFieldY = textField.frame.origin.y; i_textFieldHeight = textField.frame.size.height; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } #pragma mark- 键盘通知事件 [核心代码] //注册键盘监听消息 -(void)registerKeyBoardNotification { //增加监听,当键盘出现或改变时收出消息 [核心代码] [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //增加监听,当键退出时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object: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 } //当键盘出现或改变时调用 - (void)keyboardWillShow:(NSNotification *)aNotification { //获取键盘的高度 NSDictionary *userInfo = [aNotification userInfo]; NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [aValue CGRectValue]; int keyboardHeight = keyboardRect.size.height; //计算偏移量 i_offset = keyboardHeight - (self.view.frame.size.height-(i_textFieldY+i_textFieldHeight)); //进行偏移 NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyBoard" context:nil]; [UIView setAnimationDuration:animationDuration]; float width = self.view.frame.size.width; float height = self.view.frame.size.height; if(i_offset > 0) { CGRect rect = CGRectMake(0.0f,StautsBarHeight-i_offset,width,height); //把整个view 往上提,肯定要用负数 y self.view.frame = rect; } [UIView commitAnimations]; } //当键退出时调用 - (void)keyboardWillHide:(NSNotification *)aNotification { if(i_offset > 0) { //恢复到偏移前的正常量 NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyBoard" context:nil]; [UIView setAnimationDuration:animationDuration]; float width = self.view.frame.size.width; float height = self.view.frame.size.height; CGRect rect = CGRectMake(0.0f,StautsBarHeight,width,height); //把整个view 往上提,肯定要用负数 y 注意self.view 的y 是从20开始的,即StautsBarHeight self.view.frame = rect; [UIView commitAnimations]; } i_offset = 0; }