ios - 修改 tableFooterView 到 tableView 的最后一个单元格和 tableView 的边距的距离

标签 ios swift uitableview

我目前有一个tableView。在其 ViewController 中,在 viewDidLoad 中,我执行以下操作:

public override function viewDidLoad() {
    let footerView = bundler.loadNibNamed("FooterView")
    tableView.tableFooterView = footerView
    tableView.tableFooterView.preservesSuperViewLayoutMargins = true
    tableView.tableFooterView.backgroundColor = UIColor.black


}

问题一: 如何设法增加 tableFooterView 到最后一个单元格的距离?更改我的单元格的 layoutMargin 就完成了这项工作。更改 footerView.layoutMargin 没有任何效果,到最后一个单元格的距离非常小。

问题二:

无论是 footerView.preservesSuperViewLayoutMargins = true 还是设置 layoutMargins 都没有效果,我的 footerView 一直扩展到 tableView 的左右边距。如何让 footerView 遵守 tableView 的边距?

编辑 - 完整代码:

这是 FooterView.xib 文件的重要部分:

<view contentMode="scaleToFill" id="W6V-Yi-de6"  customModuleProvider="target">
        <rect key="frame" x="0.0" y="0.0" width="431" height="32"/>
        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
        <subviews>
                <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="tJT-kB-OPd">
                <rect key="frame" x="0.0" y="0.0" width="355" height="32"/>
                <state key="normal" title="bla bla bla">
                <color key="titleColor" white="0.66666666669999997" alpha="1" colorSpace="calibratedWhite"/>
                </state>
                <connections> …  </connections>
                 </button>
        </subviews>
 …
 </view

这是最终代码:

public override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let footerView = FooterView.loadNibNamed("FooterView")
    footerView.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40.0)
    let frame = footerView.frame

    tableView.tableFooterView = footerView

    let backgroundView = UIView()
    backgroundView.backgroundColor = .green

    let containerView = UIView(frame: CGRect(x: 10, y: 10, width: frame.width - 20, height: frame.height - 10))
    containerView.backgroundColor = .red
    backgroundView.addSubview(containerView)

    footerView.backgroundView = backgroundView
}

这是生成的图像:

footerView

有谁知道将 footerView(基本上是文本 bla bla bla)置于红色 containerView 中间的热点?

最佳答案

您可以在 FooterView 中使用容器 View,它与 cell/tableView 具有所需的边距。看下面的例子,

class MyFooter: UITableViewHeaderFooterView {}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let tableViewFrame = tableView.frame    
    let footer = MyFooter()
    footer.frame = CGRect(x: 0, y: 0, width: tableViewFrame.width, height: 150)
    tableView.tableFooterView = footer

    let frame = footer.frame
    let backgroundView = UIView()
    backgroundView.backgroundColor = .green


    let containerView = UIView(frame: CGRect(x: 8, y: 8, width: frame.width - 16, height: frame.height - 16))
    containerView.backgroundColor = .red
    backgroundView.addSubview(containerView)

    footer.backgroundView = backgroundView
}

下面是输出

enter image description here

您可以更改 containerView x、y、width 和 height 来调整边距。

一些笔记

1) viewDidLoad 不是设置约束的好地方。最好用上面的方法。

2)更改 FooterView 的背景颜色已被弃用,因此最好不要更改它。

tableView.tableFooterView.backgroundColor = UIColor.black 

关于ios - 修改 tableFooterView 到 tableView 的最后一个单元格和 tableView 的边距的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50927391/

相关文章:

ios - 当我不创建 NSTimer 对象时如何使计时器无效

iOS 5.0 sendEvent 调用了两 [2] 次

xcode - 在 UIAlertAction 中返回 int 或在 UIAlertAction 之后返回 int

ios - 当我使用自动布局选择每个单元格的高度(因此它们是动态的)时,它通常可以工作,但有些单元格有点短。为什么是这样?

ios - 单击 Cell 中的 Button 获取 UITableViewCell 的 indexPath

使用 Expo 和 Firebase 的 iOS 应用奖励推荐

ios - Else 语句中的 AlertView

ios - 如何检测 iOS 中的文本(字符串)语言?

ios - AFNetworking 自定义 API 管理器

uitableview - UITableView在批量更新中删除,插入和移动顺序