当前位置:编程学习 > wap >>

IOS开发(21)关于UITextField的键盘遮挡问题

1 前言
平时做App的时候总会遇到,UITextField的键盘会遮挡住下面的内容,由于IOS没有自己的机制,所以需要自己写方法来控制,今天我们就介绍一种简单的方法,来应对键盘遮挡问题。

2 代码实例
ZYViewController.h

 

[plain]
#import <UIKit/UIKit.h> 
 
@inte易做图ce ZYViewController : UIViewController<UITextFieldDelegate> 
 
@property(nonatomic,strong) UITextField *myTextField; 
 
@end 

#import <UIKit/UIKit.h>

@inte易做图ce 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];
   
   
&nbs

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,