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

来源:互联网 发布:算命网站源码免费下载 编辑:程序博客网 时间:2024/04/29 11:14

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

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

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

第一种,

临时调整窗口中各个视图的大小,使得键盘从下向上占领的区域空白。键盘的高度( 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];

}

 

0 0
原创粉丝点击