メッセージアプリを作ろうと思い、TextFieldに文字入力をしようとした時はビューを上にずらすという動きを実装しようとしたのですが、MisterFusionの部分でエラーになってしまいます。
参考にしたURL
http://hack.aipo.com/archives/7958/
http://qiita.com/yuto_aka_ike/items/1d0cc3f7bc3aa4d4161a
https://github.com/szk-atmosphere/MisterFusion/blob/master/README.md
Swift2
1import UIKit 2import MisterFusion 3 4class MessageViewController: UIViewController, UITextViewDelegate, UITableViewDelegate{ 5 6 var isObserving = false 7 8 var messageTextViewHeight: NSLayoutConstraint! 9 var messageTextView: UITextView! 10 11 var tableView: UITableView! 12 var underBarView: UIView! 13 var sendBtn: UIButton! 14 15 override func viewWillAppear(animated: Bool) { 16 // Viewの表示時にキーボード表示・非表示を監視するObserverを登録する 17 super.viewWillAppear(animated) 18 if(!isObserving) { 19 let notification = NSNotificationCenter.defaultCenter() 20 notification.addObserver(self, selector: "keyboardWillShow:" 21 , name: UIKeyboardWillShowNotification, object: nil) 22 notification.addObserver(self, selector: "keyboardWillHide:" 23 , name: UIKeyboardWillHideNotification, object: nil) 24 isObserving = true 25 } 26 } 27 override func viewDidLoad() { 28 29 //メッセージ表示テーブルView 30 //let frame_tableView:CGRect = CGRect(x: x, y: y, width: width, height: height) 31 tableView = UITableView() 32 //tableView.backgroundColor = .redColor() 33 //tableView = UITableView() 34 tableView.delegate = self 35 //self.view.addSubview(tableView) 36 37 underBarView = UIView() 38 39 //メッセージ表示テーブルadd 40 self.view.addLayoutSubview(tableView, andConstraints: 41 tableView.Top |-| 0, 42 tableView.Right |-| 0, 43 tableView.Left |+| 0, 44 /* 45 エラーになる部分はじまり 46 */ 47 tableView.Bottom |==| underBarView.Top |-| 0 48 /* 49 エラーになる部分おわり 50 */ 51 52 /* 53 これに変えればエラーにならない はじまり 54 */ 55 //tableView.Bottom |-| 50 56 /* 57 これに変えればエラーにならない おわり 58 */ 59 60 ) 61 print("メッセージ表示テーブルAdd成功") 62 63 //下部裏のView 64 underBarView = UIView() 65 //self.view.addSubview(underBarView) 66 underBarView.frame.size.height = 50 67 underBarView.backgroundColor = UIColor .redColor() 68 69 //メッセージ入力欄 70 messageTextView = UITextView() 71 messageTextView.delegate = self 72 self.underBarView.addSubview(messageTextView) 73 74 //送信ボタン 75 sendBtn = UIButton() 76 messageTextViewHeight = NSLayoutConstraint() 77 self.underBarView.addSubview(sendBtn) 78 79 //下部裏親のViewAdd 80 self.view.addLayoutSubview(underBarView, andConstraints: 81 underBarView.Top |==| tableView.Bottom |+| 0, 82 underBarView.Right |-| 0, 83 underBarView.Left |+| 0, 84 underBarView.Bottom |-| 0 85 ) 86 print("下部親View Add成功") 87 88 } 89 func textViewDidChange(textView: UITextView) { 90 let maxHeight = 80.0 // 入力フィールドの最大サイズ 91 if(messageTextView.frame.size.height.native < maxHeight) { 92 let size:CGSize = messageTextView.sizeThatFits(messageTextView.frame.size) 93 messageTextViewHeight.constant = size.height 94 } 95 } 96 override func viewWillDisappear(animated: Bool) { 97 // Viewの表示時にキーボード表示・非表示時を監視していたObserverを解放する 98 super.viewWillDisappear(animated) 99 if(isObserving) { 100 let notification = NSNotificationCenter.defaultCenter() 101 notification.removeObserver(self) 102 notification.removeObserver(self 103 , name: UIKeyboardWillShowNotification, object: nil) 104 notification.removeObserver(self 105 , name: UIKeyboardWillHideNotification, object: nil) 106 isObserving = false 107 } 108 } 109 func keyboardWillShow(notification: NSNotification?) { 110 // キーボード表示時の動作をここに記述する 111 let rect = (notification?.userInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 112 let duration:NSTimeInterval = notification?.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! Double 113 UIView.animateWithDuration(duration, animations: { 114 let transform = CGAffineTransformMakeTranslation(0, -rect.size.height) 115 self.view.transform = transform 116 },completion:nil) 117 } 118 func keyboardWillHide(notification: NSNotification?) { 119 // キーボード消滅時の動作をここに記述する 120 let duration = (notification?.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! Double) 121 UIView.animateWithDuration(duration, animations:{ 122 self.view.transform = CGAffineTransformIdentity 123 }, 124 completion:nil) 125 } 126} 127
TableViewを上にずらしたいので、TableViewのBottomの値を下の文字入力エリアにくっつけるようにしたいのですが、そのようなコードを書くとエラーになってしまいます。
よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。