在iOS9上编辑UITextField时上移键盘

在iOS9上编辑UITextField时上移键盘

本文介绍了在iOS9上编辑UITextField时上移键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了让我的键盘在我的iOS应用程序中向上移动以发现 UITextField ,我过去常常实现这个答案:它现在已经完美运行了。但是在iOS 9.1上,它不再起作用。

For my keyboards to move up to uncover UITextField in my iOS app, I used to implement this answer: https://stackoverflow.com/a/6908258/3855618 on iOS7 and 8 and it has worked perfectly for now. However on iOS 9.1, it doesn't work anymore.

为了更准确,即使背景视图确实向上移动, UITextField 没有。

To be more accurate, even if the background view does move up, the UITextField doesn't.

了解自iOS9和iOS 9.1以来发生了多大变化的事情?

Any idea of what has changed so much since iOS9 and iOS 9.1?

推荐答案

不推荐您链接的答案。您不应直接设置视图控制器视图的框架,尤其是在使用自动布局时。您应该将滚动视图作为子视图添加到视图中,而不是更改视图的框架,并在显示或隐藏键盘时调整内容插入。

The answer you have linked is not recommended. You should not set the view controller view's frame directly, especially not if you are using auto layout. Instead of changing the view's frame you should add a scrollview as a subview to the view, and adjust the content inset when the keyboard is shown or hidden.

来自官方苹果:

From the official apple doc:

调整内容通常涉及暂时调整一个或多个视图的大小并将其定位以便文本对象仍然可见。使用键盘管理文本对象的最简单方法是将它们嵌入到UIScrollView对象(或其子类之一,如UITableView)中。显示键盘时,您所要做的就是重置滚动视图的内容区域并将所需的文本对象滚动到位。因此,为了响应UIKeyboardDidShowNotification,您的处理程序方法将执行以下操作:

Adjusting your content typically involves temporarily resizing one or more views and positioning them so that the text object remains visible. The simplest way to manage text objects with the keyboard is to embed them inside a UIScrollView object (or one of its subclasses like UITableView). When the keyboard is displayed, all you have to do is reset the content area of the scroll view and scroll the desired text object into position. Thus, in response to a UIKeyboardDidShowNotification, your handler method would do the following:


  1. 获取键盘的大小。

  2. 按键盘高度调整滚动视图的底部内容插入。

  3. 将目标文本字段滚动到视图中。




 // 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 app might not need or want this behavior.
   CGRect aRect = self.view.frame;
   aRect.size.height -= kbSize.height;
   if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
     [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
   }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
   UIEdgeInsets contentInsets = UIEdgeInsetsZero;
   scrollView.contentInset = contentInsets;
   scrollView.scrollIndicatorInsets = contentInsets;
}

这篇关于在iOS9上编辑UITextField时上移键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 21:41