1 前言
平時做App的時候總會遇到,UITextField的鍵盤會遮擋住下面的內容,由於IOS沒有自己的機制,所以需要自己寫方法來控制,今天我們就介紹一種簡單的方法,來應對鍵盤遮擋問題。
2 代碼實例
ZYViewController.h
[plain]
#import <UIKit/UIKit.h>
@interface ZYViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic,strong) UITextField *myTextField;
@end
#import <UIKit/UIKit.h>
@interface ZYViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic,strong) UITextField *myTextField;
@end
ZYViewController.m
[plain]
@synthesize myTextField;
- (void)viewDidLoad
{
[super viewDidLoad];
//Do any additional setup after loading the view, typically from a nib.
//self.view.backgroundColor = [UIColor underPageBackgroundColor];
myTextField = [[UITextField alloc] init];//初始化UITextField
myTextField.frame = CGRectMake(35, 230, 250, 35);
myTextField.delegate = self;//設置代理
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//垂直居中
myTextField.placeholder = @"Please entry your content!";//內容為空時默認文字
myTextField.returnKeyType = UIReturnKeyDone;//設置放回按鈕的樣式
myTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;//設置鍵盤樣式為數字
[self.view addSubview:myTextField];
//注冊鍵盤出現與隱藏時候的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboadWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
//添加手勢,點擊屏幕其他區域關閉鍵盤的操作
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
gesture.numberOfTapsRequired = 1;//手勢敲擊的次數
[self.view addGestureRecognizer:gesture];
}
//鍵盤出現時候調用的事件
-(void) keyboadWillShow:(NSNotification *)note{
NSDictionary *info = [note userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//鍵盤的frame
CGFloat offY = (460-keyboardSize.height)-myTextField.frame.size.height;//屏幕總高度-鍵盤高度-UITextField高度
[UIView beginAnimations:nil context:NULL];//此處添加動畫,使之變化平滑一點
[UIView setAnimationDuration:0.3];//設置動畫時間 秒為單位
myTextField.frame = CGRectMake(35, offY, 250, 35);//UITextField位置的y坐標移動到offY
[UIView commitAnimations];//開始動畫效果
}
//鍵盤消失時候調用的事件
-(void)keyboardWillHide:(NSNotification *)note{
[UIView beginAnimations:nil context:NULL];//此處添加動畫,使之變化平滑一點
[UIView setAnimationDuration:0.3];
myTextField.frame = CGRectMake(35, 230, 250, 35);//UITextField位置復原
[UIView commitAnimations];
}
//隱藏鍵盤方法
-(void)hideKeyboard{
[myTextField resignFirstResponder];
}
#pragma mark -
#pragma mark UITextFieldDelegate
//開始編輯:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
//點擊return按鈕所做的動作:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];//取消第一響應
return YES;
}
//編輯完成:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];//移除觀察者
}
@synthesize myTextField;
- (void)viewDidLoad
{
[super viewDidLoad];
//Do any additional setup after loading the view, typically from a nib.
//self.view.backgroundColor = [UIColor underPageBackgroundColor];
myTextField = [[UITextField alloc] init];//初始化UITextField
myTextField.frame = CGRectMake(35, 230, 250, 35);
myTextField.delegate = self;//設置代理
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//垂直居中
myTextField.placeholder = @"Please entry your content!";//內容為空時默認文字
myTextField.returnKeyType = UIReturnKeyDone;//設置放回按鈕的樣式
myTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;//設置鍵盤樣式為數字
[self.view addSubview:myTextField];
//注冊鍵盤出現與隱藏時候的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboadWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
//添加手勢,點擊屏幕其他區域關閉鍵盤的操作
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
gesture.numberOfTapsRequired = 1;//手勢敲擊的次數
[self.view addGestureRecognizer:gesture];
}
//鍵盤出現時候調用的事件
-(void) keyboadWillShow:(NSNotification *)note{
NSDictionary *info = [note userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//鍵盤的frame
CGFloat offY = (460-keyboardSize.height)-myTextField.frame.size.height;//屏幕總高度-鍵盤高度-UITextField高度
[UIView beginAnimations:nil context:NULL];//此處添加動畫,使之變化平滑一點
[UIView setAnimationDuration:0.3];//設置動畫時間 秒為單位
myTextField.frame = CGRectMake(35, offY, 250, 35);//UITextField位置的y坐標移動到offY
[UIView commitAnimations];//開始動畫效果
}
//鍵盤消失時候調用的事件
-(void)keyboardWillHide:(NSNotification *)note{
[UIView beginAnimations:nil context:NULL];//此處添加動畫,使之變化平滑一點
[UIView setAnimationDuration:0.3];
myTextField.frame = CGRectMake(35, 230, 250, 35);//UITextField位置復原
[UIView commitAnimations];
}
//隱藏鍵盤方法
-(void)hideKeyboard{
[myTextField resignFirstResponder];
}
#pragma mark -
#pragma mark UITextFieldDelegate
//開始編輯:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
//點擊return按鈕所做的動作:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];//取消第一響應
return YES;
}
//編輯完成:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];//移除觀察者
}
運行結果:
初始狀態
單擊輸入框後