2016-08-09 4 views
0

uitableviewの下部に貼り付けるテキストフィールドと送信ボタンを追加したいと思います。これを実現するために、テーブルビューにフッターを追加しましたが、フッターがuitableviewの最下部ではなく最後のコメントの下に表示されているようです。また、キーボードが表示されているときにはフッターを上に移動し、キーボードが外されると下に移動したいと思う。下に私のコードを参照することができます。ありがとう!Instrumentコメントセクションのように、uitableviewの下部にテキストフィールドを作成してボタンを送信してください

Expected Result i.e. instagram/chat app textfield at bottom

Expected Result

Actual outcome

Actual outcome

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
    footer = UIView(frame: CGRectMake(0, 0, 320, 44)) 
    footer!.backgroundColor = UIColor.lightGrayColor() 

    textField = UITextField(frame: CGRectMake(5, 5, 315, 35)) 
    textField!.borderStyle = UITextBorderStyle.Bezel 

    yourSendButton = UIButton(frame: CGRectMake(318,5,62,35)) 
     // [[UIButton alloc] initWithFrame:CGRectMake(225,5,75,35)]; 
    yourSendButton!.backgroundColor = UIColor.lightGrayColor() 
    yourSendButton!.setTitle("Send", forState: .Normal) 
    yourSendButton!.setTitleColor(UIColor.darkGrayColor(), forState: .Normal) 
    yourSendButton!.addTarget(self, action: "sendBtnClicked:", forControlEvents: UIControlEvents.TouchUpInside) 
    //addTarget:self action:@selector(sendBtnClicked) forControlEvents:UIControlEventTouchUpInside]; 

    footer!.addSubview(yourSendButton!) 
    footer!.addSubview(textField!) 

    self.navigationController?.view.addSubview(footer!) 

    return footer 
} 

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    return 44 
} 

func sendBtnClicked (sender: UIButton) { 
    print("send button pressed") 
    print(textField?.text) 
} 

答えて

0

私は、このコードはあなたを助けることができるカスタムのUIViewControllerでのtableViewを使用することをお勧め:

var commentField:UITableViewCell! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil) 
} 

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(true) 
    NSNotificationCenter.defaultCenter().removeObserver(self) 
} 

func keyboardWillAppear(notification: NSNotification){ 

    var userInfo:NSDictionary = notification.userInfo! 
    var keyboardSize:CGSize = userInfo.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size 

    var contentInsets:UIEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0) 

    self.tableView.contentInset = contentInsets 
    self.tableView.scrollIndicatorInsets = contentInsets 

    var messageFrame:CGRect = self.commentField.frame 
    messageFrame.origin.y -= keyboardSize.height 
    self.commentField.frame = messageFrame 

} 

func keyboardWillDisappear(notification: NSNotification){ 
    var userInfo:NSDictionary = notification.userInfo! 
    var keyboardSize:CGSize = userInfo.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size 

    UIView.beginAnimations(nil, context: nil) 
    UIView.setAnimationDuration(0.25) 
    self.tableView.contentInset = UIEdgeInsetsZero 
    UIView.commitAnimations() 

    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero 

    var messageFrame:CGRect = self.commentField.frame 
    messageFrame.origin.y += keyboardSize.height 
    self.commentField.frame = messageFrame 
} 
+0

私が間違っていない場合は、viewcontroller内にテーブルビューを追加します。 Tableviewセルがコメントフィールドになるようですが、どこにテキストフィールドを追加してボタンを送信しますか?ありがとう。 – mir

+0

あなたのviewcontrollerにuitableviewを追加してからコメントを含むセルを追加し、uituitviewの下にテキストフィールドと送信ボタンを置くだけでよいので、uituitviewの高さと幅をストーリーボードで変更でき、tableviewデータソースを実装するのを忘れていますhep :)もし私の答えが私の答えを確認することを躊躇しなかったなら:)感謝 –

関連する問題