我不知道如何描述我的问题,所以这里有一个视频:
ios - Swift-tableView的顶部为背景色-LMLPHP
我有一个tableViewController我希望橙色是红色,顶部是红色,底部是白色。
我试图改变背景颜色(橙色),但这不是我能喜欢的。。。
你知道这样做是否可行吗,因为有时红色部分的文本可能很短,我们可以看到tableViewController的顶部和底部

最佳答案

下面是代码示例。为视图控制器更改它。

import UIKit

class ViewController: UIViewController {

// Count your number of cells from your array. 3 its just example.
private let numberOfRedCells: CGFloat = 3
private let cellHeight: CGFloat = 50

var defaultOffSet: CGPoint?
var defaultHeightConstraint: NSLayoutConstraint!

lazy var tableView: UITableView = {
    let tableView  = UITableView(frame: self.view.frame)
    // Important. Clear backgroundColor.
    tableView.backgroundColor = .clear
    tableView.separatorStyle = .none
    tableView.dataSource = self
    tableView.delegate = self
    return tableView
}()

lazy var redView: UIView = {
    let rv = UIView()
    rv.backgroundColor = .red
    return rv
}()

override func viewDidLoad() {
    super.viewDidLoad()


    tableView.register(UITableViewCell.self, forCellReuseIdentifier: NSStringFromClass(UITableViewCell.self))

    view.addSubview(redView)
    setupConstraints()
    view.addSubview(tableView)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    defaultOffSet = tableView.contentOffset
}

private func setupConstraints() {
    redView.translatesAutoresizingMaskIntoConstraints = false
    let top = NSLayoutConstraint(item: redView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0)
    let left = NSLayoutConstraint(item: redView, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1, constant: 0)
    let right = NSLayoutConstraint(item: redView, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1, constant: 0)
    let bottom = NSLayoutConstraint(item: redView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
    view.addConstraints([top, left, right, bottom])
}

}

extension ViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 8
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.self), for: indexPath)
    // Your red type cells, equals 3 just for example
    if indexPath.row < 3 {
        cell.backgroundColor = .red
        cell.textLabel?.text = "Input your text here"
    } else {
        // Your white type cells
        cell.backgroundColor = .white
        cell.textLabel?.text = "Your information"
    }

    return cell
}

}

extension ViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return cellHeight
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let contentYoffset = scrollView.contentOffset.y + 20 // 20 - statusBar height

    if contentYoffset > 0 {
        self.redView.frame.size.height = 20
    } else {
        self.redView.frame.size.height = cellHeight * numberOfRedCells - contentYoffset
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}

}

这就是我所拥有的。enter link description here

10-05 20:20
查看更多