如何向静态 UITableView 添加页脚?

问题描述 投票:0回答:2

我有一个设置屏幕,我想在我的 static

UITableView
中添加一个页脚,其中包含应用程序的版本号:

我读过很多关于 SO 的帖子,其中绝大多数似乎都是 Objective-C 和/或使用

UITableView
等在带有动态内容的
tableView(_:titleForHeaderInSection:)
中添加页脚。我找到了一个问题的答案,建议简单地使用
UITableView.tableFooterView
属性,所以我也尝试过这个,但仍然没有得到任何结果,如上图所示。

这是我所做的:

override func viewDidLoad() {
    super.viewDidLoad()

    setUpView()
  }

  func setUpView() {
    self.navigationController?.navigationBar.topItem?.title = "Settings"

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.dismissSettings(_:)))

    formulasTitle.textColor = UIColor.black
    liftsTitle.textColor = UIColor.black
    roundNumbersTitle.textColor = UIColor.black
    sendFeedbackTitle.textColor = UIColor.black
    unitsTitle.textColor = UIColor.black
    pleaseRateTitle.textColor = UIColor.black
    legalMumboJumboTitle.textColor = UIColor.black

    currentFormulaSelection.detailTextLabel!.text = settingsViewModel.defaultFormulaName
    currentLiftsSelection.detailTextLabel!.text = settingsViewModel.defaultLiftName
    weightUnitControl.selectedSegmentIndex = settingsViewModel.weightUnitSelectedSegmentIndex
    roundNumbersSwitch.isOn = settingsViewModel.roundNumbersSwitchOn

    roundNumbersSwitch.addTarget(self, action: #selector(self.roundingOptionDidChange), for: .valueChanged)
    weightUnitControl.addTarget(self, action: #selector(weightUnitDidChange), for: .valueChanged)

    let versionLabel = UILabel()
    versionLabel.text = getVersion()
    let footer = UITableViewHeaderFooterView()

    tableView.tableFooterView = footer

  }

  func getVersion() -> String {
    let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0"
    let appBuild = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0"

    let versionLabelText: String
    versionLabelText = "Version \(appVersion) (\(appBuild))"

    return versionLabelText
   }

使用

tableView.tableFooterView
是正确的解决方案,而我只是做得不正确,还是以其他方式添加页脚?

编辑#1

我根据塔拉斯的回答添加了这个,但仍然没有:

override func viewDidLoad() {
    super.viewDidLoad()

    let versionLabel = UILabel()
    versionLabel.text = getVersion()

    let footer = UITableViewHeaderFooterView(reuseIdentifier: "footer")
    tableView.tableFooterView = footer
    tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "footer")
    footer.frame = CGRect(x: 50, y: 10, width: 100, height: 100)
    footer.contentView.addSubview(versionLabel)
    footer.backgroundView?.backgroundColor = UIColor.green
    footer.constrainToSuperView()
  }

extension UIView {
  func constrainToSuperView() {
    guard let superview = superview else {
      return
    }
    translatesAutoresizingMaskIntoConstraints = false
    topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
    bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
    leadingAnchor.constraint(equalTo: superview.leadingAnchor).isActive = true
    trailingAnchor.constraint(equalTo: superview.trailingAnchor).isActive = true
  }
}

需要注意的两个奇怪的事情:在运行时,

footer
的框架是
(50.0, 535.333333333333, 375.0, 100.0)
。另外,如果我这样做,我会看到带有版本号的标签,但它左对齐,我似乎无法将其居中:

 footer.textLabel?.text = versionLabel.text
swift uitableview footer
2个回答
2
投票
let footer = UITableViewHeaderFooterView()

tableView.tableFooterView = footer

在这两行中,您创建了零帧的空页脚。尝试使用下一个:

let footer = UIView(frame: CGRect(x:0, y:0, width: UIScreen.main.bound.width, height: 100))
footer.backgroundColor = UIColor.red
tableView.tableFooterView = footer

0
投票

@tarasChernyshenko 的答案是正确的,但我认为您需要更具体的内容才能获得与您上传的图像相同的结果。

为此,您可以将具有零框架的 UIView 设置为 tableView 页脚,然后为您的视图设置适当的背景颜色。

类似这样的:

tableView.tableFooterView = UIView()
view.backgroundColor = .groupTableViewBackground

请注意,部分标题颜色从背景更改,这是正常的情况,但如果您想要相同的颜色,只需为标题部分设置清晰的背景颜色即可。如果您没有为标题部分设置自定义视图,则只需覆盖此功能并且重要的是,设置为清除背景视图的背景颜色。

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let view = view as? UITableViewHeaderFooterView {
        view.backgroundView?.backgroundColor = .clear
    }
}

通过故事板定位页脚视图,您需要在底部放置一个零帧的视图,然后为此设置一个调整大小蒙版,固定并在所有侧面调整大小。其背景颜色并不重要,因为永远不会显示。

作为数据,标题部分的背景颜色默认为

UIExtendedSRGBColorSpace 0.920532 0.920532 0.926863 1
,但可能会因型号或操作系统而改变。

© www.soinside.com 2019 - 2024. All rights reserved.