我在视图底部显示的TableView
添加了一个页脚。但是,除非我向下滚动,否则页脚不可见,然后显示。
我还没有填充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 :(得分: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
层次结构分开。