gpt4 book ai didi

ios - UIKit - Swift - 允许 tableHeaderView 向上滚动,但不能向下滚动

转载 作者:行者123 更新时间:2023-11-29 06:01:37 25 4
gpt4 key购买 nike

我有一个带有导航栏和标签栏的 UIViewController。除此之外,整个屏幕由 UITableView 组成。

我有一个大的 tableHeaderView,其背景颜色与导航栏相同。

当我向上拖动内容(向下滚动)时,一切看起来都很好。

但是如果我将其向上拖动,导航栏和标题 View 之间就会出现丑陋的脱节。

有什么方法可以在向下拖动时将其锚定到顶部,同时允许它在向上拖动时滚动吗?

enter image description here

最佳答案

您可以尝试创建一个 View 并将其放置在tableView后面,当 TableView 滚动时, View 的高度会更新。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

lazy var tableView : UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.dataSource = self
tableView.delegate = self
return tableView
}()

let backView : UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()

var backViewHeight : NSLayoutConstraint?

override func viewDidLoad() {
super.viewDidLoad()

self.title = "ViewController"

self.view.addSubview(backView)
backView.translatesAutoresizingMaskIntoConstraints = false
backView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
backView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
backView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
backViewHeight = backView.heightAnchor.constraint(equalToConstant: 0)
backViewHeight?.isActive = true

self.view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
tableView.register(Cell.self, forCellReuseIdentifier: "cell")
tableView.register(Header.self, forHeaderFooterViewReuseIdentifier: "header")
tableView.backgroundColor = .clear

self.navigationController?.navigationBar.barTintColor = .red
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")

}

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


func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y < 0 {
backViewHeight?.constant = -scrollView.contentOffset.y
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
return cell
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header")
header?.contentView.backgroundColor = .red
let headerLabel = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 100))
headerLabel.textAlignment = .center
headerLabel.text = "Header"
header?.addSubview(headerLabel)
return header
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100
}

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = .white
return view
}

}

class Cell: UITableViewCell {

let label : UILabel = {
let label = UILabel()
label.text = "One Label"
return label
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.backgroundColor = .clear
setupViews()
}

func setupViews() {
self.backgroundColor = .white
self.addSubview(label)
label.frame = self.frame
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

class Header : UITableViewHeaderFooterView {


override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

如果将此代码复制粘贴到空项目中,您可以查看其行为。不要忘记将 ViewController 嵌入到 NavigationController 中。希望对您有帮助

关于ios - UIKit - Swift - 允许 tableHeaderView 向上滚动,但不能向下滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54611704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com