简体   繁体   English

Swift-tableView的顶部为背景色

[英]Swift - tableView with a background color for the top

I don't really know how to describe my problem so here is a video: 我真的不知道如何描述我的问题,所以这里有一个视频:

在此处输入图片说明

I have a tableViewController and I would like that the orange color to be red a the top and white at the bottom. 我有一个tableViewController ,我希望橙色是顶部是红色,底部是白色。 I tried to change the background color (in orange) and it's not what I could like... 我试图更改背景颜色(橙色),这不是我想要的...

Do you know if it's possible to do that, given that sometimes, the text in the red section can be really short, and we can see both the top and the bottom part of the tableViewController 您是否知道是否可以这样做,因为有时红色部分中的文字可能会很短,我们可以看到tableViewController的顶部和底部

Add a red view to the same parent view of the table view and set position to be at the top of tableview. 将红色视图添加到表视图的同一父视图中,并将位置设置在表视图的顶部。 its width should be tablview view's width and height should initially be 0. Using scroll view delegate and set the red view's height to max(-scrollView.contentOffset.y, 0). 其宽度应为tablview视图的宽度,而高度最初应为0。使用滚动视图委托并将红色视图的高度设置为max(-scrollView.contentOffset.y,0)。 Set the table view background color to white. 将表格视图背景色设置为白色。

Here is the code example. 这是代码示例。 Change it for your view controller. 为您的视图控制器更改它。

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)
}

}

Here is what I've got. 这就是我所拥有的。 enter link description here 在此处输入链接说明

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

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