IOS 软键盘不遮挡UITextField方法

  经常在有多个文本输入的界面出现软键盘遮挡UITextFiled的情况,解决方法如下:

1.设置UITextFiled类的代理

2.实现UITextFiledDelegate中的

- (void)textFieldDidBeginEditing:(UITextField *)textField和

- (BOOL)textFieldShouldReturn:(UITextField *)textField方法

具体示例如下:

  UITextField *userNameTextField = [[UITextFieldalloc]initWithFrame:CGRectMake(90,240,190, 30)];

  userNameTextField.borderStyle =UITextBorderStyleRoundedRect;

  userNameTextField.delegate =self;




- (void)textFieldDidBeginEditing:(UITextField *)textField

 {

     CGRect frame = textField.frame;

     // int offset = frame.origin.y +422 - (self.view.frame.size.height - 240);//键盘高度216

     int offset = frame.origin.y +200 - (self.view.frame.size.height -240);//键盘高度216

     NSLog(@"offset is %d",offset);

     NSTimeInterval animationDuration =0.30f;

      [UIViewbeginAnimations:@"ResizeForKeyBoard"context:nil];

      [UIViewsetAnimationDuration:animationDuration];

     float width =self.view.frame.size.width;

     float height =self.view.frame.size.height;

     if(offset >0){

        CGRect rect =CGRectMake(0.0f, -offset,width,height);

        self.view.frame = rect;

      }

     [UIViewcommitAnimations];

}



- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

      NSTimeInterval animationDuration =0.30f;

       [UIViewbeginAnimations:@"ResizeForKeyboard"context:nil];

       [UIViewsetAnimationDuration:animationDuration];

       CGRect rect =CGRectMake(0.0f,0.0f,self.view.frame.size.width,self.view.frame.size.height);

      self.view.frame = rect;

       [UIViewcommitAnimations];

       [textFieldresignFirstResponder];

       returnYES;

}


针对ios5.0之后键盘高度随输入法不同变化时的解决办法,同时也适用于ios5.0之前

在-(void)viewDidLoad方法里面添加通知

复制代码
1   if(IOS_VERSION<5.0)
2     {
3         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
4         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillHideNotification object:nil];
5     }else{
6         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
7     }
复制代码

至于如何获取IOS系统版本号通过如下可获取

#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

方法-(void)keyboardWillShow:(NSNotification *)notification具体如下

复制代码
-(void)keyboardWillShow:(NSNotification *)notification
{

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
            NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
#else
            NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];
#endif
            CGRect keyboardBounds;
            [keyboardBoundsValue getValue:&keyboardBounds];
            NSInteger offset =self.view.frame.size.height-keyboardBounds.origin.y+64.0;
            CGRect listFrame = CGRectMake(0, -offset, self.view.frame.size.width,self.view.frame.size.height);
            NSLog(@"offset is %d",offset);
            [UIView beginAnimations:@"anim" context:NULL];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationBeginsFromCurrentState:YES];
            [UIView setAnimationDuration:0.3];
            //处理移动事件,将各视图设置最终要达到的状态
    
            self.view.frame=listFrame;
            
            [UIView commitAnimations];
            
        }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值