关于IOS键盘遮住输入框以及关闭的方法。

本文介绍了在iOS应用中,当键盘弹出时如何避免遮挡输入框,并提供了解决方案,包括增加页面高度和调整坐标。同时,讲解了自定义UITextField来实现关闭键盘的方法。
摘要由CSDN通过智能技术生成

一。在普通的View界面 

1.当点击输入框,弹出键盘挡住输入框,可用增加页面高度,调整坐标。其中count = 0的操作看具体需求。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{ //当点触textField内部,开始编辑都会调用这个方法。textField将成为first responder 
    if(count == 0){
        //当出现键盘时,添加frame的高度,已便键盘的使用
        NSTimeInterval animationDuration = 0.30f;  
        CGRect frame = self.view.frame;
        frame.origin.y -=130;
        frame.size.height +=130;
        self.view.frame = frame;
        [UIView beginAnimations:@"ResizeView" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;                
        [UIView commitAnimations];          
        count++;
        
    }
}

2.关闭键盘,点击背景出发 touchesBegan事件,通过取消输入框第一响应来关闭键盘,并将页面高度和坐标还原。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if (![touch.view isKindOfClass:[UITextField class]]) {
        [self.loginText resignFirstResponder];
        [self.pwdText resignFirstResponder];
    }
    if(count != 0){
        
        NSTimeInterval anima
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值