将 UITextView 放在键盘上?

Placing UITextView on top of keyboard?

当我的视图控制器弹出时,我已经调用了键盘。我只想 将 UITextView 直接放在键盘上方。 我可以对其进行硬编码,但担心键盘尺寸不同。有什么想法吗?

@IBOUtlet var textView: UITextView! = UITextView()

您可以使用 UIKeyboardDidShowNotification 通知来了解键盘何时启动。您可以使用此通知中的键盘框架来动态计算此 UITextField 的位置。

override func viewDidLoad() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
}

func keyBoardDidShow(notification :NSNotification)
{
   if let frameObject: AnyObject = notification.userInfo?[UIKeyboardFrameEndUserInfoKey]
   {
        let keyboardRect = frameObject.CGRectValue()
        // use keyboardRect to calculate the frame of the textfield
   }
}

阅读更多信息 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/index.html#//apple_ref/doc/constant_group/Keyboard_Notification_User_Info_Keys