xcode 13.0,手机系统 iOS15, push卡顿(图片如下)
- 设置navigationBar背景颜色
navBar.backgroundColor = .white -
在navigationBar添加一个view,覆盖statusBar背景颜色
statusBarView = UIView(frame: CGRect(x: 0, y: -kStatusBarHeight, width: SCREEN_WIDTH, height: kStatusBarHeight))
statusBarView.backgroundColor = .white
self.navigationBar.addSubview(statusBarView)
image.png
import UIKit
class BaseNavigationController: UINavigationController {
var statusBarView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let navBar = UINavigationBar.appearance()
navBar.backgroundColor = .white
// 设置false之后自动下沉navigationBar 高度
self.navigationBar.isTranslucent = false
// 设置navigationBar的背景色
navBar.barTintColor = .white
// 设置左右bar的颜色
navBar.tintColor = UIColor(white: 51/255.0, alpha: 1)
// 消除 navigation 阴影 (即 navigation 下面的黑线)
navBar.shadowImage = UIImage()
// 设置标题的样式
let attriDic = [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 18),NSAttributedString.Key.foregroundColor:UIColor(white: 16/255.0, alpha: 1)]
navBar.titleTextAttributes = attriDic
statusBarView = UIView(frame: CGRect(x: 0, y: -kStatusBarHeight, width: SCREEN_WIDTH, height: kStatusBarHeight))
statusBarView.backgroundColor = .white
self.navigationBar.addSubview(statusBarView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
// 不是第一级vc
if viewControllers.count > 0 {
viewController.hidesBottomBarWhenPushed = true
// 可以在这里设置返回按钮等,重写后侧滑返回失效需要自己单独处理
// backBarButtonItem 是带有字和返回箭头的样式
viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "arrow_back"), style: .plain, target: self, action: #selector(navBackAction))
self.interactivePopGestureRecognizer?.delegate = self as? UIGestureRecognizerDelegate
}
//一定要写在最后,要不然无效
super.pushViewController(viewController, animated: animated)
}
// 返回操作
@objc func navBackAction(){
popViewController(animated: true)
}
}