1 前言
平时做App的时候总会遇到,UITextField的键盘会遮挡住下面的内容,由于IOS没有自己的机制,所以需要自己写方法来控制,今天我们就介绍一种简单的方法,来应对键盘遮挡问题。
2 代码实例
ZYViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ZYViewController : UIViewController<UITextFieldDelegate> 4 5 @property(nonatomic,strong) UITextField *myTextField; 6 7 @end 8 9 #import <UIKit/UIKit.h> 10 11 @interface ZYViewController : UIViewController<UITextFieldDelegate> 12 13 @property(nonatomic,strong) UITextField *myTextField; 14 15 @end
ZYViewController.m
1 @synthesize myTextField; 2 3 - (void)viewDidLoad 4 { 5 [super viewDidLoad]; 6 //Do any additional setup after loading the view, typically from a nib. 7 //self.view.backgroundColor = [UIColor underPageBackgroundColor]; 8 myTextField = [[UITextField alloc] init];//初始化UITextField 9 myTextField.frame = CGRectMake(35, 230, 250, 35); 10 myTextField.delegate = self;//设置代理 11 myTextField.borderStyle = UITextBorderStyleRoundedRect; 12 myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//垂直居中 13 myTextField.placeholder = @"Please entry your content!";//内容为空时默认文字 14 myTextField.returnKeyType = UIReturnKeyDone;//设置放回按钮的样式 15 myTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;//设置键盘样式为数字 16 [self.view addSubview:myTextField]; 17 18 19 //注册键盘出现与隐藏时候的通知 20 [[NSNotificationCenter defaultCenter] addObserver:self 21 selector:@selector(keyboadWillShow:) 22 name:UIKeyboardWillShowNotification 23 object:nil]; 24 [[NSNotificationCenter defaultCenter] addObserver:self 25 selector:@selector(keyboardWillHide:) 26 name:UIKeyboardWillHideNotification 27 object:nil]; 28 //添加手势,点击屏幕其他区域关闭键盘的操作 29 UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)]; 30 gesture.numberOfTapsRequired = 1;//手势敲击的次数 31 [self.view addGestureRecognizer:gesture]; 32 } 33 34 //键盘出现时候调用的事件 35 -(void) keyboadWillShow:(NSNotification *)note{ 36 NSDictionary *info = [note userInfo]; 37 CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//键盘的frame 38 CGFloat offY = (460-keyboardSize.height)-myTextField.frame.size.height;//屏幕总高度-键盘高度-UITextField高度 39 [UIView beginAnimations:nil context:NULL];//此处添加动画,使之变化平滑一点 40 [UIView setAnimationDuration:0.3];//设置动画时间 秒为单位 41 myTextField.frame = CGRectMake(35, offY, 250, 35);//UITextField位置的y坐标移动到offY 42 [UIView commitAnimations];//开始动画效果 43 44 } 45 //键盘消失时候调用的事件 46 -(void)keyboardWillHide:(NSNotification *)note{ 47 [UIView beginAnimations:nil context:NULL];//此处添加动画,使之变化平滑一点 48 [UIView setAnimationDuration:0.3]; 49 myTextField.frame = CGRectMake(35, 230, 250, 35);//UITextField位置复原 50 51 [UIView commitAnimations]; 52 } 53 //隐藏键盘方法 54 -(void)hideKeyboard{ 55 [myTextField resignFirstResponder]; 56 } 57 #pragma mark - 58 #pragma mark UITextFieldDelegate 59 //开始编辑: 60 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 61 { 62 return YES; 63 } 64 65 //点击return按钮所做的动作: 66 - (BOOL)textFieldShouldReturn:(UITextField *)textField 67 { 68 [textField resignFirstResponder];//取消第一响应 69 return YES; 70 } 71 72 //编辑完成: 73 - (void)textFieldDidEndEditing:(UITextField *)textField 74 { 75 76 } 77 78 -(void)viewDidDisappear:(BOOL)animated{ 79 [super viewDidDisappear:animated]; 80 [[NSNotificationCenter defaultCenter] removeObserver:self];//移除观察者 81 }
运行结果:
单击输入框后