夫特3+: 我有采取的视图成TextView的背景和设定的视图(前置,底部,固定的高度)该约束。创建底部约束@IBOutlet和管理,下面的代码:
class ViewController: UIViewController {
@IBOutlet var bottomConstraint: NSLayoutConstraint!
@IBOutlet var view_TextViewBg: UIView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardDidShow(_:)),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardDidHide(_:)),
name: NSNotification.Name.UIKeyboardWillHide,
object: nil)
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
// tap.delegate = self
view.addGestureRecognizer(tap)
}
func handleTap(sender: UITapGestureRecognizer? = nil) {
//dissmiss your keyboard here
}
//MARK: Keyboard show
func keyboardDidShow(_ notification: Notification) {
let params = notification.userInfo
let rect: CGRect? = (params?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
bottomConstraint.constant = (rect?.size.height)!
}
//MARK: Keyboard hide
func keyboardDidHide(_ notification: Notification) {
bottomConstraint.constant = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}