简体   繁体   English

迅速4 UITableView页脚,绝对底部

[英]swift 4 UITableView Footer, absolute bottom

I a have created a footer for my UITableView like so: 我已经为UITableView创建了页脚,如下所示:

let customView = UIView(frame: CGRect(x: 0, y: self.view.bounds.height - 50, width: self.view.bounds.width, height: 50))
            customView.backgroundColor = UIColor.red

            commentText = UITextField(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width - 50, height: 50))
            commentText.backgroundColor = UIColor.white
            commentText.layer.borderColor = UIColor.gray.cgColor
            commentText.layer.borderWidth = 1
            commentText.borderStyle = .roundedRect
            commentText.placeholder = "Enter Comment"

            let submitButton = UIButton(frame: CGRect(x: self.tableView.bounds.width - 50, y: 0, width: 50, height: 50))
            submitButton.setTitle("Go", for: .normal)
            submitButton.backgroundColor = UIColor.blue
            submitButton.addTarget(self, action: #selector(self.submitButtonPressed(_:)), for: .touchUpInside)

            customView.addSubview(commentText)
            customView.addSubview(submitButton)

            tableView.tableFooterView = customView

and here is the result of it in the screenshot: 这是屏幕截图中的结果:

在此处输入图片说明

What I am trying to do is have the footer always at the bottom of the screen, is this possible? 我想做的是将页脚始终放在屏幕底部,这可能吗?

A table view footer is always at the end of the table view, after the last row in the table view. 表格视图页脚始终位于表格视图的末尾,在表格视图的最后一行之后。 This is true no matter how many rows are in the table view. 无论表视图中有多少行,都是如此。

If you want a view that stays at the bottom of the screen and never scrolls no matter how many rows there are, then you don't want a table view footer. 如果您希望视图停留在屏幕底部,并且无论有多少行都从不滚动,则您不希望使用表格视图页脚。 You simply want a view added to the bottom of the screen. 您只需要将视图添加到屏幕底部。

The best solution is to use a UIViewController that has your "footer view" pinned to the bottom and a table view added that fills the rest of the screen. 最好的解决方案是使用UIViewController ,该UIViewController的“页脚视图”固定在底部,并添加一个表格视图以填充屏幕的其余部分。

也许您可以只在屏幕视图的底部添加页脚视图,而不能将其添加为UITableView的页脚视图,因为您不希望在UITableView滚动时移动文本字段和提交按钮。

您可以仅将自定义视图添加到视图本身,而不是tableview。

view.addSubView(customView)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM