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

    在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 = [userInfo objectForKey: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.widthviewFooter.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.heightviewFooter.frame.size.widthviewFooter.frame.size.height);

        } completion:^(BOOL finished){

            

        }];

    }

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

    [[NSNotificationCenter defaultCenter] removeObserver:self];
     
     
     
     
     
  • 相关阅读:
    移动桌面文件
    软件项目经理素质能力的必备要求
    如何管理时间
    《明日歌》
    浏览网页出现iexplore.exe应用程序错误为:"0x03e620b0"指令引用的"0x00000000"内存.该内存不能为"read"?
    css网站布局学习笔记
    因为爱,人生除了理智,还有情感!
    35岁之前成功的12条黄金法则
    VS2005中没有DataGrid控件的解决方案
    先装VS2005再装IIS,出现访问IIS元数据库失败解决方案
  • 原文地址:https://www.cnblogs.com/Cristen/p/2789755.html
Copyright © 2011-2022 走看看