[英]How do I show the keyboard with a view above it?
当用户点击一个按钮时,我希望键盘弹出(这很容易),但是我希望视图随之上升(粘在键盘顶部)。 该视图将具有“发送消息..”文本字段。 当用户按下完成时,我希望键盘与视图一起消失。
如何使该视图“粘”在键盘上?
转到您的情节提要,然后在viewController的底部添加一个视图(将其称为topKeyboardView)。 并赋予以下约束:
底部空间到底部布局= 0
然后添加textfield *((我更喜欢使用textView使其在消息变得太长时更改其高度...)*,并在topKeyboardView顶部添加您的按钮(发送)。
现在让代码..转到您的viewController.swift并将IBOutlet添加到textField和按钮并添加以下功能:
//this is will tell if the keyboard hidden or not
func addKeyboardNotifications() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil)
}
// MARK:- Notification
func keyboardWillShow(notification: NSNotification) {
print("keyboard is up")
}
func keyboardWillHide(notification: NSNotification) {
print("keyboard is down")
}
在您的viewDidLoad
调用函数:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
addKeyboardNotifications()
}
运行...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.