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

1 前言

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

2 代码实例

ZYViewController.h

#import <UIKit/UIKit.h>

@interface ZYViewController : UIViewController<UITextFieldDelegate>

@property(nonatomic,strong) UITextField *myTextField;

@end

ZYViewController.m

@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];//移除观察者
}

运行结果:

初始状态


单击输入框后


3 结语

以上就是对键盘遮挡的处理方案,希望对大家有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值