一个UITableView头部添加一个UIView,都用snpkit(Swift代码)布局,一般代码写法
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(listView)
listView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
listView.tableHeaderView = headBackView
headBackView.snp.makeConstraints { make in
make.width.equalTo(listView.snp.width)
make.height.equalTo(100)
}
}
很明显,view遮住了cell,原因是:
- 在viewDidLoad中,
listView
添加headerView时,headBackView
的frame并没有生成,listView.tableHeaderView
高度为0 - 后面刷新布局时,
headBackView
高度改变已经不能影响listView.tableHeaderView
的布局了 - 因此,需要线确定
headBackView
的frame,再给listView.tableHeaderView
赋值
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(headBackView)
headBackView.snp.makeConstraints { make in
make.top.left.right.equalToSuperview()
make.height.equalTo(100)
}
self.view.addSubview(listView)
listView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
// 在布局完成后的方法中,再操作
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if listView.tableHeaderView == nil {
headBackView.removeFromSuperview()
listView.tableHeaderView = headBackView
headBackView.snp.remakeConstraints { make in
make.width.equalTo(listView.snp.width)
make.height.equalTo(100)
}
}
}
在某个时间点,头部view高度变化
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if listView.tableHeaderView == nil {
headBackView.removeFromSuperview()
listView.tableHeaderView = headBackView
headBackView.snp.remakeConstraints { make in
make.width.equalTo(listView.snp.width)
make.height.equalTo(100)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.headBackView.snp.updateConstraints { make in
make.height.equalTo(200)
}
}
}
}
view还是遮挡了
listView
,但现在istView.tableHeaderView
的高度有100,也就是说:listView.tableHeaderView
被赋值后,snpkit不能在改变它的布局当
headView
发生高度变化时,只能先取消istView.tableHeaderView
赋值,添加到self.view中,更改布局,再赋值给istView.tableHeaderView
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if listView.tableHeaderView == nil {
headBackView.removeFromSuperview()
listView.tableHeaderView = headBackView
headBackView.snp.remakeConstraints { make in
make.width.equalTo(listView.snp.width)
make.height.equalTo(100)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.listView.tableHeaderView = nil
self.view.addSubview(self.headBackView)
self.headBackView.snp.remakeConstraints { make in
make.width.equalTo(0)
make.height.equalTo(300)
}
// 添加一个延时,再去刷新
DispatchQueue.main.asyncAfter(deadline: .now() + 0.001) {
self.headBackView.removeFromSuperview()
self.listView.tableHeaderView = self.headBackView
self.headBackView.snp.remakeConstraints { make in
make.width.equalTo(self.listView.snp.width)
make.height.equalTo(300)
}
}
}
}
}