• 如何保持iOS上键盘出现时输入框不被覆盖


    在 iOS5 上请求显示键盘时,系统从屏幕底部将键盘滑入上来,位于应用的内容之上。

    (墙内:http://mikixiyou.iteye.com/blog/1488302)

    如果屏幕中的内容项目比较多,它就可能覆盖住文本输入框之类的对象。你必须调整你的内容,使得输入框保持可见。

    你会想到哪些处理方法呢?

    第一种,

    临时调整窗口中各个视图的大小,使得键盘从下向上占领的区域空白。键盘的高度( keyboard.size.height )是一定的,将视图中所有内容所在区域的 y 值减小到 y-keyboard.size.height 。

    该方法有个局限,如果所有内容之和大于窗口减去键盘高度的话,该方法将不能用。

    第二种,

    将窗口中所有视图嵌入进一个滚动视图对象( UIScrollView )中。在键盘出现时,你将输入框滚动到合适的位置,调整一下滚动视图的内容区域。

    这些操作通过一个通知 UIKeyboardDidShowNotification 去实现的,逻辑过程如下:

    1 、根据通知的字典信息 userInfo 得到键盘的 size 。

    2 、根据键盘的 size 中的 height 值,调整滚动视图内容底部的 inset 。

    3 、滚动目标视图即文件输入框进入视图中。

    简要的代码如下:

    1 、实现两个委托方法,用于指定输入框对象。

    - (void)textFieldDidBeginEditing:(UITextField *)textField

    {

        activeField = textField;

    }

     

    - (void)textFieldDidEndEditing:(UITextField *)textField

    {

        activeField = nil;

    }

    2 、注册通知的观察者

    - (void)registerForKeyboardNotifications

    {

        [[NSNotificationCenter defaultCenter] addObserver:self

                selector:@selector(keyboardWasShown:)

                name:UIKeyboardDidShowNotification object:nil];

     

       [[NSNotificationCenter defaultCenter] addObserver:self

                 selector:@selector(keyboardWillBeHidden:)

                 name:UIKeyboardWillHideNotification object:nil];

     

    }

    将这个方法放在 viewDidAppear 中调用。

    同时也要写一个 removeObserver 放在 viewWillDisappear 中调用。

    3 、实现键盘显示通知的 selector 中的方法

    // Called when the UIKeyboardDidShowNotification is sent.

    - (void)keyboardWasShown:(NSNotification*)aNotification

    {

        NSDictionary* info = [aNotification userInfo];

        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

     

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);

        scrollView.contentInset = contentInsets;

        scrollView.scrollIndicatorInsets = contentInsets;

     

        // If active text field is hidden by keyboard, scroll it so it's visible

        // Your application might not need or want this behavior.

        CGRect aRect = self.view.frame;

        aRect.size.height -= kbSize.height;

        if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {

            CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);

            [scrollView setContentOffset:scrollPoint animated:YES];

        }

    }

    4 、实现键盘消失通知的方法

    // Called when the UIKeyboardWillHideNotification is sent

    - (void)keyboardWillBeHidden:(NSNotification*)aNotification

    {

        UIEdgeInsets contentInsets = UIEdgeInsetsZero;

        scrollView.contentInset = contentInsets;

        scrollView.scrollIndicatorInsets = contentInsets;

    }

    这个方法调整内容底部的 inset 的值使得输入框不被键盘区域屏蔽的。还可以换种方法实现。

    第三种,

    扩展内容视图的高度,滚动文本输入框对象进内容视图。

    将 keyboardWasShown: 重写。

    - (void)keyboardWasShown:(NSNotification*)aNotification {

        NSDictionary* info = [aNotification userInfo];

        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

        CGRect bkgndRect = activeField.superview.frame;

        bkgndRect.size.height += kbSize.height;

        [activeField.superview setFrame:bkgndRect];

        [scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES];

    }

  • 相关阅读:
    【问题】SUSE已经安装了libsodium,安装zeromq时出现下面的错误?
    将博客搬至CSDN
    iOS开发之self.abc = nil与[_abc release]的区别
    Swift编程语言入门教程
    如何使用NSData处理数据
    集合之間的轉換
    數組排序
    集合类的用法总结(NSArray、NSDictionary、NSSet)
    NSString的各种用法总结(创建、截取、判断比较、转化数据类型、拼接、替换、添加、追加、读取、写入、删去、改变)
    instancetype和id的区别
  • 原文地址:https://www.cnblogs.com/lovewx/p/3851172.html
Copyright © 2020-2023  润新知