2017-08-26 79 views
0

我在頁面底部顯示的TableView添加了一個頁腳。但是,除非我向下滾動,否則頁腳不可見,然後顯示。頁腳不會顯示在TableView上,除非我滾動

這裏就是我說的: Invisible Footer

我沒有填充TableView,但我不認爲這是一個問題。下面是我爲頁腳編寫的代碼:

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

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 

    footerView.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(footerView) 

    //x, y, w, h constraints 
    footerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 
    footerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true 
    footerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true 
    footerView.heightAnchor.constraint(equalToConstant: 50).isActive = true 

    //Send Button 
    let sendButton = UIButton(type: .system) 
    sendButton.setTitle("Send", for: .normal) 
    sendButton.translatesAutoresizingMaskIntoConstraints = false 
    sendButton.addTarget(self, action: #selector(handleSend), for: .touchUpInside) 
    footerView.addSubview(sendButton) 

    //x, y, w, h Button constraints 
    sendButton.rightAnchor.constraint(equalTo: footerView.rightAnchor).isActive = true 
    sendButton.centerYAnchor.constraint(equalTo: footerView.centerYAnchor).isActive = true 
    sendButton.widthAnchor.constraint(equalToConstant: 80).isActive = true 
    sendButton.heightAnchor.constraint(equalTo: footerView.heightAnchor).isActive = true 

    //Text Field 
    footerView.addSubview(inputTextField) 

    //x, y, w, h Text Field constraints 
    inputTextField.leftAnchor.constraint(equalTo: footerView.leftAnchor, constant: 8).isActive = true 
    inputTextField.centerYAnchor.constraint(equalTo: footerView.centerYAnchor).isActive = true 
    inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true 
    inputTextField.heightAnchor.constraint(equalTo: footerView.heightAnchor).isActive = true 

    // Separator 
    let separatorLineView = UIView() 
    separatorLineView.backgroundColor = UIColor(red: CGFloat(220/255), green: CGFloat(220/255), blue: CGFloat(220/255), alpha: 0.1) 
    separatorLineView.translatesAutoresizingMaskIntoConstraints = false 
    footerView.addSubview(separatorLineView) 

    //x, y, w, h separator constraints 
    separatorLineView.leftAnchor.constraint(equalTo: footerView.leftAnchor).isActive = true 
    separatorLineView.topAnchor.constraint(equalTo: footerView.topAnchor).isActive = true 
    separatorLineView.widthAnchor.constraint(equalTo: footerView.widthAnchor).isActive = true 
    separatorLineView.heightAnchor.constraint(equalToConstant: 1).isActive = true 

    return footerView 
} 

我想頁腳的工作是儘快用戶導航到視圖展現出來,然後向上移動的鍵盤顯示出來的方式,並在鍵盤被解散時向下移動。

回答

0

您不應該將footerView作爲子視圖添加到另一個UIView

您應該刪除下面的代碼:

footerView.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(footerView) 

    //x, y, w, h constraints 
    footerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 
    footerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true 
    footerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true 
    footerView.heightAnchor.constraint(equalToConstant: 50).isActive = true 

UITableView將處理頁腳視圖的位置。

如果我懷疑你打算將footerView固定在視圖的底部,那麼你不應該將它作爲表視圖的頁腳視圖返回。它應該與UITableView分層結構分開。

+0

你能否詳述一下你最後一句話? 如果我不應該返回頁腳視圖,我應該返回什麼?也就是說,如果我應該返回任何東西。 –