@@ -36,46 +36,52 @@ class ViewController: UIViewController {
3636 NotificationCenter . default. addObserver ( self , selector: #selector( ViewController . keyboardWillShow ( _: ) ) , name: NSNotification . Name. UIKeyboardWillShow, object: nil )
3737
3838 NotificationCenter . default. addObserver ( self , selector: #selector( ViewController . keyboardWillHide ( _: ) ) , name: NSNotification . Name. UIKeyboardWillHide, object: nil )
39-
40- growingTextView. layer. cornerRadius = 4
41- growingTextView. backgroundColor = UIColor ( white: 0.9 , alpha: 1 )
42- growingTextView. textContainerInset = UIEdgeInsets ( top: 16 , left: 0 , bottom: 4 , right: 0 )
43- growingTextView. placeholderAttributedText = NSAttributedString ( string: " Placeholder text " ,
44- attributes: [ NSFontAttributeName: growingTextView. font!,
39+
40+ self . growingTextView. layer. cornerRadius = 4
41+ self . growingTextView. backgroundColor = UIColor ( white: 0.9 , alpha: 1 )
42+ self . growingTextView. textContainerInset = UIEdgeInsets ( top: 16 , left: 0 , bottom: 4 , right: 0 )
43+ self . growingTextView. placeholderAttributedText = NSAttributedString ( string: " Placeholder text " ,
44+ attributes: [ NSFontAttributeName: self . growingTextView. font!,
4545 NSForegroundColorAttributeName: UIColor . gray
4646 ]
4747 )
48+
4849 }
4950
5051 override func didReceiveMemoryWarning( ) {
5152 super. didReceiveMemoryWarning ( )
5253 // Dispose of any resources that can be recreated.
5354 }
5455 override func touchesBegan( _ touches: Set < UITouch > , with event: UIEvent ? ) {
55- view. endEditing ( true )
56+ self . view. endEditing ( true )
5657 }
5758
59+
5860 @IBAction func handleSendButton( _ sender: AnyObject ) {
59- growingTextView. text = " "
60- view. endEditing ( true )
61+ self . growingTextView. text = " "
62+ self . view. endEditing ( true )
6163 }
6264
6365
6466 func keyboardWillHide( _ sender: Notification ) {
65-
66- //key point 0,
67- inputContainerViewBottom. constant = 0
68- //textViewBottomConstraint.constant = keyboardHeight
69- UIView . animate ( withDuration: 0.25 , animations: { ( ) -> Void in self . view. layoutIfNeeded ( ) } )
67+ if let userInfo = ( sender as NSNotification ) . userInfo {
68+ if let _ = ( userInfo [ UIKeyboardFrameEndUserInfoKey] as? NSValue ) ? . cgRectValue. size. height {
69+ //key point 0,
70+ self . inputContainerViewBottom. constant = 0
71+ //textViewBottomConstraint.constant = keyboardHeight
72+ UIView . animate ( withDuration: 0.25 , animations: { ( ) -> Void in self . view. layoutIfNeeded ( ) } )
73+ }
74+ }
7075 }
7176 func keyboardWillShow( _ sender: Notification ) {
72- guard let keyboardHeight = ( sender. userInfo ? [ UIKeyboardFrameEndUserInfoKey] as? NSValue ) ? . cgRectValue. size. height else {
73- return
74- }
75- inputContainerViewBottom. constant = keyboardHeight
76- UIView . animate ( withDuration: 0.25 , animations: { ( ) -> Void in
77- self . view. layoutIfNeeded ( )
78- } )
77+ if let userInfo = ( sender as NSNotification ) . userInfo {
78+ if let keyboardHeight = ( userInfo [ UIKeyboardFrameEndUserInfoKey] as? NSValue ) ? . cgRectValue. size. height {
79+ self . inputContainerViewBottom. constant = keyboardHeight
80+ UIView . animate ( withDuration: 0.25 , animations: { ( ) -> Void in
81+ self . view. layoutIfNeeded ( )
82+ } )
83+ }
84+ }
7985 }
8086}
8187
0 commit comments