为啥我的 UITableViewController 页脚高度不会改变?
Posted
技术标签:
【中文标题】为啥我的 UITableViewController 页脚高度不会改变?【英文标题】:Why won't my UITableViewController footer height change?为什么我的 UITableViewController 页脚高度不会改变? 【发布时间】:2020-05-15 22:32:40 【问题描述】:我正在 Swift Playgrounds 中编写一个模拟消息应用程序,为了保存消息,我有一个带有自定义单元格的 UITableViewController
。我正在尝试在表格中添加一个“粘性”页眉和页脚,它们将分别保存导航控件、消息输入和发送按钮。不过,我似乎无法更改页眉和页脚的高度。我尝试过使用tableView(_:heightForFooterInSection:)
& tableView(_:heightForHeaderInSection:)
,但这也不起作用。
这是我目前所拥有的:
import UIKit
import PlaygroundSupport
class ViewController: UITableViewController
var textMessages = [
"Here's my very first message",
"I'm going to message another long message that will word wrap",
"I'm going to message another long message that will word wrap, I'm going to message another long message that will word wrap, I'm going to message another long message that will word wrap",
"some messaging"
]
override func viewDidLoad()
super.viewDidLoad()
navigationItem.title = "Messages"
navigationController?.navigationBar.prefersLargeTitles = true
tableView.register(ChatMessageCell.self, forCellReuseIdentifier: "cell_1")
tableView.separatorStyle = .none
adddatextbruh()
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 75))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 75))
button.setTitle("Submit", for: .normal)
//button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
customView.addSubview(button)
return customView
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 75))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 75))
button.setTitle("Submit", for: .normal)
//button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
customView.addSubview(button)
return customView
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return textMessages.count
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_1", for: indexPath) as! ChatMessageCell
cell.selectionStyle = .none
if indexPath.row % 2 == 1
cell.messageLabel.text = textMessages[indexPath.row]
//cell.setupConstraints(side: 1)
cell.bubbleBackgroundView.backgroundColor = UIColor(white: 0.9, alpha: 1)
return cell
else
cell.messageLabel.text = textMessages[indexPath.row]
//cell.setupConstraints(side: 0)
cell.bubbleBackgroundView.backgroundColor = .blue
return cell
//let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ChatMessageCell
// cell.textLabel?.text = "We want to provide a longer string that is actually going to wrap onto the next line and maybe even a third line."
// cell.textLabel?.numberOfLines = 0
func adddatextbruh()
textMessages.append("hey whats up")
tableView.beginUpdates()
tableView.insertRows(at: [
(NSIndexPath(row: textMessages.count-1, section: 0) as IndexPath)], with: .automatic)
tableView.endUpdates()
tableView.scrollToRow(at: IndexPath(row: textMessages.count-1, section: 0), at: UITableView.ScrollPosition.bottom, animated: true)
class ChatMessageCell: UITableViewCell
let messageLabel = UILabel()
let bubbleBackgroundView = UIView()
var leadingAnchorConstant = CGFloat()
func setupConstraints(side: Int)
if side == 1
leadingAnchorConstant = frame.size.width - 176
else
leadingAnchorConstant = 32
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
super.init(style: style, reuseIdentifier: reuseIdentifier)
bubbleBackgroundView.backgroundColor = .yellow
let gradient = CAGradientLayer()
bubbleBackgroundView.layer.shadowOpacity = 0.35
bubbleBackgroundView.layer.shadowRadius = 6
bubbleBackgroundView.layer.shadowOffset = CGSize(width: 0, height: 0)
bubbleBackgroundView.layer.shadowColor = UIColor.black.cgColor
bubbleBackgroundView.layer.cornerRadius = 25
bubbleBackgroundView.translatesAutoresizingMaskIntoConstraints = false
addSubview(bubbleBackgroundView)
addSubview(messageLabel)
// messageLabel.backgroundColor = .green
messageLabel.text = "We want to provide a longer string that is actually going to wrap onto the next line and maybe even a third line."
messageLabel.numberOfLines = 0
messageLabel.translatesAutoresizingMaskIntoConstraints = false
// lets set up some constraints for our label
let constraints = [messageLabel.topAnchor.constraint(equalTo: topAnchor, constant: 32),
messageLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 32),
messageLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -32),
messageLabel.widthAnchor.constraint(equalToConstant: 250),
bubbleBackgroundView.topAnchor.constraint(equalTo: messageLabel.topAnchor, constant: -16),
bubbleBackgroundView.leadingAnchor.constraint(equalTo: messageLabel.leadingAnchor, constant: -16),
bubbleBackgroundView.bottomAnchor.constraint(equalTo: messageLabel.bottomAnchor, constant: 16),
bubbleBackgroundView.trailingAnchor.constraint(equalTo: messageLabel.trailingAnchor, constant: 16),
]
NSLayoutConstraint.activate(constraints)
// messageLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
PlaygroundPage.current.liveView = ViewController()
【问题讨论】:
【参考方案1】:跟随功能不是为了高度
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
<#code#>
下一个函数是高度。
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
return your_height
【讨论】:
【参考方案2】:解决了。我只是在 viewDidLoad() 中设置了tableView.sectionFooterHeight = //the height
。不知道为什么 heightForFooterInSection/headerInSection 不起作用,但对我来说这无论如何都要干净得多。
【讨论】:
因为高度需要用到这个函数:func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat return 50以上是关于为啥我的 UITableViewController 页脚高度不会改变?的主要内容,如果未能解决你的问题,请参考以下文章
如何呈现UICollectionViewController?
在 Swift Core Data 中从一对多关系中获取对象