ios解决软键盘遮挡输入框问题

 

//尝试了好长时间,终于测试搞定!这里screenHeight,textFieldFromButtomHeigth需要具体情况具体分析,和视图布局有关

//解决因屏幕旋转而产生的问题。这下无论竖屏还是横屏键盘都不会遮挡输入框了。

-(void)textFieldDidBeginEditing:(UITextField *)textField

{


    float screenHeight; //屏幕尺寸,如果屏幕允许旋转,可根据旋转动态调整

    float keyboardHeight = 216; //键盘尺寸,如果屏幕允许旋转,可根据旋转动态调整

    

    textField.delegate =self;

    

    if ([[UIScreen mainScreen] applicationFrame].size.height==1024) {//此时为横屏

        screenHeight = [[UIScreen mainScreen] applicationFrame].size.width;//748-50

    }else{

        //screenHeight = 880;

        screenHeight = [[UIScreen mainScreen] applicationFrame].size.height-50;//1004-50

    }

    

    

    NSLog(@"screenHeight=%f",screenHeight);

    

    float statusBarHeight,NavBarHeight,tableCellHeight,textFieldOriginY,textFieldOriginX,textFieldFromButtomHeigth;

    int margin;

    statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height; //屏幕状态栏高度

    NavBarHeight = self.navigationController.navigationBar.frame.size.height; //获取导航栏高度

    

    UITableViewCell *tableViewCell=(UITableViewCell *)textField.superview;

    tableCellHeight = tableViewCell.frame.size.height; //获取单元格高度

    

    CGRect fieldFrame=[self.view convertRect:textField.frame fromView:tableViewCell];

    textFieldOriginY = fieldFrame.origin.y; //获取文本框相对本视图的y轴位置。

    textFieldOriginX = fieldFrame.origin.x; //获取文本框相对本视图的x轴位置。

    NSLog(@"textFieldOriginX=%f",textFieldOriginX);

    // NSLog(@"textFieldOriginY=%f",textFieldOriginY);

    // NSLog(@"tableCellHeight=%f",tableCellHeight);

    // NSLog(@"NavBarHeight=%f",NavBarHeight);

    // NSLog(@"statusBarHeight=%f",statusBarHeight);

    

    //计算文本框到屏幕底部的高度(屏幕高度-顶部状态栏高度-导航栏高度-文本框的的相对y轴位置-单元格高度)

    if ([[UIScreen mainScreen] applicationFrame].size.height==1024) {//此时为横屏

        textFieldFromButtomHeigth = screenHeight - 140 - NavBarHeight - textFieldOriginY - tableCellHeight;

        

        NSLog(@"screenHeight=%f",screenHeight);

        NSLog(@"statusBarHeight=%f",statusBarHeight);

        NSLog(@"NavBarHeight=%f",NavBarHeight);

        NSLog(@"textFieldOriginX=%f",textFieldOriginX);

        NSLog(@"textFieldOriginY=%f",textFieldOriginY);

        NSLog(@"tableCellHeight=%f",tableCellHeight);

        

    }else{

        textFieldFromButtomHeigth = screenHeight - statusBarHeight - NavBarHeight - textFieldOriginY - tableCellHeight; //textFieldFromButtomHeigth原理还是不太清

        

        NSLog(@"screenHeight=%f",screenHeight);//screenHeight=864.000000

        NSLog(@"statusBarHeight=%f",statusBarHeight);//statusBarHeight=20.000000

        NSLog(@"NavBarHeight=%f",NavBarHeight);//NavBarHeight=44.000000

        NSLog(@"textFieldOriginY=%f",textFieldOriginY);//textFieldOriginY=873.000000

        NSLog(@"tableCellHeight=%f",tableCellHeight);//tableCellHeight=59.000000

        

    }


    if(textFieldFromButtomHeigth < keyboardHeight) { //如果文本框到屏幕底部的高度 < 键盘高度

        margin = keyboardHeight - textFieldFromButtomHeigth; // 则计算差距

        keyBoardMargin_ = margin; //keyBoardMargin_ 为成员变量,记录上一次移动的间距,用户离开文本时恢复视图高度

    } else {

        margin= 0;

        keyBoardMargin_ = 0;

    }

    NSLog(@"margin=%d",margin);

    

    //NSLog(@"keyboardHeight=%f",keyboardHeight);

    NSLog(@"textFieldFromButtomHeigth=%f",textFieldFromButtomHeigth);

    

    NSLog(@"keyBoardMargin_=%d",keyBoardMargin_);

    

    float movementDuration = 0.3f; // 动画时间

    

    int movement = -margin; //进入时根据差距移动视图,离开时恢复之前的高度

    

    [UIView beginAnimations: @"textFieldAnim" context: nil]; //添加动画

    [UIView setAnimationBeginsFromCurrentState: YES];

    [UIView setAnimationDuration: movementDuration];

    self.view.frame = CGRectOffset(self.view.frame, 0, movement);

    [UIView commitAnimations];


}


//输入框编辑完成以后,将视图恢复到原始状态

-(void)textFieldDidEndEditing:(UITextField *)textField

{

    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值