zoukankan      html  css  js  c++  java
  • iOS5 切换中文键盘时覆盖输入框的完美解决方案

    iOS5 切换中文键盘时覆盖输入框的完美解决方案  

    原文:http://wangsheng2008love.blog.163.com/blog/static/782016892012631102714562/

    2012-07-31 22:42:43|  分类: iOS |  标签:ios5  中文键盘  遮盖  输入框  |举报|字号 订阅

     
     

    众所周知,iOS5之前,iPhone上的键盘的高度是固定为216.0px高的,中文汉字的选择框是悬浮的,所以不少应用都将此高度来标注键盘的高度(包括米聊也是这么做的)。

      可是在iOS5中,键盘布局变了,尤其是中文输入时,中文汉字选择框就固定在键盘上方,这样就使得原本与键盘紧密贴合的界面视图被中文汉字选择框给覆盖住了。一方面影响了界面的美观,另一方面,如果被覆盖的部分就是文本输入框的话,用户就无法看到输入的内容了。因此这个问题就必须得解决了。

    解决方法

      其实在一开始使用216.0px这个固定值来标注键盘的高度就是错误的。因为在iOS3.2以后的系统中,苹果就提供了键盘使用的API以及Demo程序——“KeyboardAccessory”。

      处理键盘事件的正确方法是这样的:(包括获取键盘的位置以及键盘弹出和消失动画的时间)

      1)在要使用键盘的视图控制器中(既viewDidLoad中),接收键盘事件的通知:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    // 键盘高度变化通知,ios5.0新增的 
    #ifdef __IPHONE_5_0
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0) {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    #endif

    2)然后添加键盘事件的处理代码:

        获取到当前keyboard的高度以及动画时间,然后对视图进行对应的操作即可。

    #pragma mark -
    #pragma mark Responding to keyboard events
    - (void)keyboardWillShow:(NSNotification *)notification {
    /*

         Reduce the size of the text view so that it's not obscured by the keyboard.

         Animate the resize so that it's in sync with the appearance of the keyboard.

         */

        NSDictionary *userInfo = [notification userInfo];

        // Get the origin of the keyboard when it's displayed.

        NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

        // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.

        CGRect keyboardRect = [aValue CGRectValue];

        // Get the duration of the animation.

        NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

        NSTimeInterval animationDuration;

        [animationDurationValue getValue:&animationDuration];

        

        // Animate the resize of the text view's frame in sync with the keyboard's appearance.

        [UIView animateWithDuration:animationDuration animations:^{

    //此处的viewFooter即是你的输入框View

      //20为状态栏的高度

      self.viewFooter.frame = CGRectMake(viewFooter.frame.origin.x, keyboardRect.origin.y-20-viewFooter.frame.size.height,viewFooter.frame.size.width, viewFooter.frame.size.height);

        } completion:^(BOOL finished){

            

        }];

    }


    - (void)keyboardWillHide:(NSNotification *)notification {
    NSDictionary* userInfo = [notification userInfo];

        NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

        CGRect keyboardRect = [aValue CGRectValue];

        

        /*

         Restore the size of the text view (fill self's view).

         Animate the resize so that it's in sync with the disappearance of the keyboard.

         */

        [UIView animateWithDuration:0 animations:^{

            self.viewFooter.frame = CGRectMake(viewFooter.frame.origin.x, keyboardRect.origin.y-20-viewFooter.frame.size.height,viewFooter.frame.size.width, viewFooter.frame.size.height);

        } completion:^(BOOL finished){

            

        }];

    }

      3)在视图控制器消除时(即viewDidUnload中),移除键盘事件的通知:

    [[NSNotificationCenter defaultCenter] removeObserver:self];
     
     

    在一个多项输入界面上,会有多个uitextfield类型的输入框。为了滚动方面,我们会将他们一一添加到uitableview的cell中,从而组成一个可以上下滑动的数据输入界面。

    但是字符输入是通过系统自动弹出软键盘来完成的,因此在选择屏幕底端的cell时,会被软键盘的区域所覆盖。

    同样,正因为输入框是在uitableview的cell中,所以可以将所在的cell滚动到软键盘覆盖的区域之上的位置。使得我们能看到输入框。

    现在,我们要实现这个操作过程。

    我们可以简单的将该cell滚动到表视图的可见区域的最上方的位置。

    uitableview提供了这个方法“scrolltorowatindexpath:atscrollposition:animated:”。作为第一反应者的输入框,可以它的委托方法- (void)textfielddidbeginediting:(uitextfield*)textfield

    将其赋值一个变量lastedittextfield中。软键盘的出现和消失都注册到该视图控制器类中,并实现它的方法。这样,在uitextfield激活弹出键盘时,系统会自动提交一个软键盘弹出的通知。键盘弹出通知会调用一个方法。

    该方法要实现的逻辑是根据lastedittextfield所在的位置点,得到操作的cell,再将此cell滚动到视图可见区域的最上方。

    cgpoint pt =lastedittextfield.center;

    pt = [self.tableview convertpoint:ptfromview:lastedittextfield.superview];

    nsindexpath* indexpath = [self.tableviewindexpathforrowatpoint:pt];

    [self.tableview scrolltorowatindexpath:indexpathatscrollposition:uitableviewrowanimationtop animated:yes];

    这段代码中,最重要的是根据lastedittextfield.center的值,通过uiview的实例方法-(cgpoint)convertpoint:(cgpoint)point fromview:(uiview *)view 将它转成self.tableview的坐标体系下的值。再根据新坐标体系中lastedittextfield.center的值,通过uitableview的实例方法scrolltorowatindexpath:atscrollposition:animated:将该cell滚动到屏幕最上方。

    在键盘消失时,系统会再发送一个通知,将屏幕恢复到原来的位置。

    将输入框滚动到最上方,虽然能满足我们的输入可见要求。但是,如果能只滚动到软键盘之上方,而不是uitableview的最上方;在输入框不在键盘覆盖的区域时,是不需要滚动的。这样的处理,将是更满意的结果。

     
     
     
     
  • 相关阅读:
    Linux内核设计第三周学习总结 跟踪分析Linux内核的启动过程
    Linux内核设计第二周学习总结 完成一个简单的时间片轮转多道程序内核代码
    Linux内核设计第一周学习总结 计算机如何工作
    信息安全系统设计基础期末总结
    信息安全系统设计基础第十四周学习总结
    信息安全系统设计基础第十三周学习总结
    20135310陈巧然 20135305姚歌 实验四:外设驱动程序设计
    linux内核设计与实现一书阅读整理 之第一二章整合
    20135239 益西拉姆 linux内核分析 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
    20135239 益西拉姆 linux内核分析 跟踪分析Linux内核的启动过程
  • 原文地址:https://www.cnblogs.com/lvyinbentengzhe/p/4160171.html
Copyright © 2011-2022 走看看