gpt4 book ai didi

swift - 将 UITextView 放在键盘上?

转载 作者:行者123 更新时间:2023-11-28 07:12:30 24 4
gpt4 key购买 nike

当我的 View Controller 弹出时,我已经调用了键盘。我想要做的就是将 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

关于swift - 将 UITextView 放在键盘上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27883704/

24 4 0