【发布时间】:2017-08-26 05:18:56
【问题描述】:
我向我的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
}
我希望页脚的工作方式是在用户导航到视图时立即显示,然后在键盘显示时向上移动,并在键盘关闭时向下移动。
【问题讨论】:
标签: swift uitableview swift3 uiview