我正在使用带有swift2.0的iOS9。我的视图控制器处理两个文本字段和一些标签。这两个文本字段包含在stackview中,标签还包含在另一个stackview中。我已经设置了通用应用程序,包括纵向和横向选项,它的工作原理好吧。但是当我点击文本字段然后键盘就在那时键盘覆盖文本字段,所以我看不到文本字段数据。如何解决这个问题以及如何管理键盘上下的所有约束?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector:("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object:nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func keyboardWillShow(notification:NSNotification){
print("keyboardWillShow")
var info = notification.userInfo!
let keyboardFrame:CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
print(keyboardFrame)
UIView.animateWithDuration(0.1, animations: { () -> Void in
})
}
func keyboardWillHide(notification:NSNotification){
print("keyboardWillHide")
}
答案 0 :(得分:2)
func keyboardWillShow(sender: NSNotification){
var info = sender.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.view.center.y = self.view.center.y - keyboardFrame.size.height
})
}
func keyboardWillHide(sender: NSNotification){
var info = sender.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.view.center.y = self.view.center.y + keyboardFrame.size.height
})
}