最近发现头部下拉放大的代码在iOS11里的frame一直不对。所以就适配了一下,记录一下过程:
1、准备好顶部view
lazy var topView:HRDDPersonTopContentView = {
guard let view = Bundle.main.loadNibNamed("HRDDPersonTopContentView", owner: self, options: nil)?.first as? HRDDPersonTopContentView else {
return HRDDPersonTopContentView()
}
view.frame = CGRect(x: 0, y: 0, width: WIDTH, height:173*GlobalScare)
return view
}()
2、在viewdidload里设置tableview的contentInset
self.edgesForExtendedLayout = UIRectEdge()
extendedLayoutIncludesOpaqueBars = true
if #available(iOS 11.0, *){
tableView.contentInsetAdjustmentBehavior = .never
}else{
automaticallyAdjustsScrollViewInsets = false
}
self.tableView.contentInset = UIEdgeInsetsMake(173*GlobalScare, 0, 0, 0)
self.view.addSubview(self.topView)
topView.delegate = self
self.tableView.registerNibWithCell(HRDDPersonArrowTableCell.self)
3、在viewWillLayoutSubviews里重新设置一下contentInset和顶部view的的frame
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.tableView.contentInset = UIEdgeInsetsMake(173*GlobalScare, 0, 0, 0)
self.topView.frame = CGRect(x: 0, y: 0, width: WIDTH, height:173*GlobalScare)
}
4、滚动操作
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print(scrollView.frame)
print(scrollView.contentInset)
let pointy = scrollView.contentOffset.y
if pointy < -173*GlobalScare {
var rect = self.topView.frame //self.tableView.viewWithTag(102)!.frame
rect.size.height = -pointy
self.topView.frame = rect
}
}
OK,这样就完成了,这里最大的问题就是iOS 11.0的设置问题:
extendedLayoutIncludesOpaqueBars = true
if #available(iOS 11.0, *){
tableView.contentInsetAdjustmentBehavior = .never
}else{
automaticallyAdjustsScrollViewInsets = false
}
网友评论