preface
The environment
Xcode 13.0
iOS 15.0
Copy the code
The source code is available on Github and can be viewed directly at the bottom of the article
The solution
There are two main properties (UINavigationController properties)
// Static style
self.navigationBar.standardAppearance;
// Scroll styles
self.navigationBar.scrollEdgeAppearance;
Copy the code
The main processing code for UINavigationController is listed below. If you want to see it all, download the source code at the bottom of this article
No headlines
import UIKit
/// Do not use headings
class TestOneNC: UINavigationController {
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
print(#function);
self.xq_init()
}
override init(nibName nibNameOrNil: String? .bundle nibBundleOrNil: Bundle?). {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
print(#function);
self.xq_init()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print(#function);
self.xq_init()
}
func xq_init(a) {
print(#function);
self.navigationBar.prefersLargeTitles = false
self.navigationBar.barTintColor = .red;
self.navigationBar.tintColor = .green
let appearance = UINavigationBarAppearance.init()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.red
appearance.shadowImage = UIImage.init()
appearance.shadowColor = UIColor.clear
self.navigationBar.standardAppearance = appearance
self.navigationBar.scrollEdgeAppearance = appearance
}
override func viewDidLoad(a) {
super.viewDidLoad()
print(#function); }}Copy the code
Use a headline
import UIKit
/// use big headings
class TestNC: UINavigationController {
var appearance_test: UINavigationBarAppearance?
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
print(#function);
self.xq_init()
}
override init(nibName nibNameOrNil: String? .bundle nibBundleOrNil: Bundle?). {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
print(#function);
self.xq_init()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print(#function);
self.xq_init()
}
func xq_init(a) {
print(#function);
self.navigationBar.prefersLargeTitles = true
self.navigationBar.barTintColor = .red;
self.navigationBar.isTranslucent = true
self.navigationBar.tintColor = .green
let appearance = UINavigationBarAppearance.init()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.red
appearance.shadowImage = UIImage.init()
appearance.shadowColor = UIColor.clear
self.appearance_test = appearance
// WXQ static style
self.navigationBar.standardAppearance = appearance
}
override func viewDidLoad(a) {
super.viewDidLoad()
print(#function);
}
func xq_scrollEdgeAppearance(_ appearance: UINavigationBarAppearance?). {
// WXQ scroll style
self.navigationBar.scrollEdgeAppearance = appearance
}
override func pushViewController(_ viewController: UIViewController.animated: Bool) {
print(#function, self.viewControllers);
if self.viewControllers.count > = 1 {
viewController.hidesBottomBarWhenPushed = true;
viewController.navigationItem.largeTitleDisplayMode = .never;
}
// WXQ can be set for the first time
if self.viewControllers.count = = 1 {
self.xq_scrollEdgeAppearance(self.appearance_test)
}
super.pushViewController(viewController, animated: animated)
}
override func popToRootViewController(animated: Bool)- > [UIViewController]? {
print(#function, self.viewControllers);
self.xq_scrollEdgeAppearance(nil)
return super.popToRootViewController(animated: animated)
}
override func popViewController(animated: Bool) -> UIViewController? {
print(#function, self.viewControllers);
if self.viewControllers.count < = 2 {
self.xq_scrollEdgeAppearance(nil)}return super.popViewController(animated: animated)
}
}
Copy the code
project
XQNavigationBar15BlankDemo project address