说一下思路: 红色就是tableview的tableHeaderView;橘色就是分区头,直接上代码
import UIKit
class BgViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var label = UILabel()
var tableView = UITableView.init(frame: CGRect.zero, style: .plain)
var navHeight: CGFloat = 0.0
var namelabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
navHeight = (self.navigationController?.navigationBar.frame.maxY)!
print(navHeight)
self.tableView.frame = CGRect(x: 0, y: 0,width: self.view.frame.size.width,height: self.view.frame.size.height)
self.view.addSubview(self.tableView)
self.tableView.delegate = self
self.tableView.dataSource = self
//注册UITableView,cellID为重复使用cell的Identifier
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
label.backgroundColor = UIColor.red
label.frame = CGRect(x: 0, y: 0,width: self.view.frame.size.width,height: 50)
label.text = "我是大标题"
label.font = UIFont.boldSystemFont(ofSize: 21)
label.textColor = UIColor.black
self.tableView.tableHeaderView = label
let titleview = UIView()
titleview.frame = CGRect(x: 0, y: 0,width: self.view.frame.size.width * 0.6,height: 40)
titleview.layer.masksToBounds = true
self.navigationItem.titleView = titleview
self.namelabel.frame = CGRect(x: 0, y: 40,width: self.view.frame.size.width * 0.6,height: 40)
self.namelabel.textColor = UIColor.black
self.namelabel.text = "我是大标题"
self.namelabel.textAlignment = .center
titleview.addSubview(self.namelabel)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = (tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)) as UITableViewCell
cell.textLabel?.text = "我是第\(indexPath.row)行"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let viw = UIView()
viw.frame = CGRect(x: 0, y: 0,width: self.view.frame.size.width,height: 40)
viw.backgroundColor = UIColor.orange
let ser = UISearchBar()
for sub in ser.subviews {
sub.subviews.first?.removeFromSuperview()
}
ser.placeholder = "搜索"
ser.frame = CGRect(x: 0, y: 0,width: 300,height: 40)
ser.center = viw.center
viw.addSubview(ser)
return viw
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//上滑时,tableview的headview完全被navigationBar遮住显示标题
if scrollView.contentOffset.y > label.frame.size.height - navHeight {
}else{
self.namelabel.frame.origin.y = -scrollView.contentOffset.y - 24
if self.namelabel.frame.origin.y <= 0 {
self.namelabel.frame.origin.y = 0
}
}
//下拉放大字体,这里的30是增大因子,不可以太小,不然字体会很大-----abs绝对值
if scrollView.contentOffset.y < -navHeight {
let font = abs(scrollView.contentOffset.y) / 30.0
label.font = UIFont.boldSystemFont(ofSize: 21 + font)
}
}
}
网友评论