我已经创建了一个自定义UINavigationController类来轻松设置我应用的导航栏。
class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationBar()
}
}
extension NavigationController {
func configureNavigationBar() {
navigationBar.tintColor = UIColor.white
navigationBar.backgroundColor = UIColor.clear
navigationBar.isTranslucent = false
navigationBar.barStyle = .black
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.shadowImage = UIImage()
let attributes = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont(name: "Merriweather-Bold", size:17)!]
navigationBar.titleTextAttributes = attributes
let gradientLayer = CAGradientLayer()
let topColor:UIColor = UIColor(hex: 0xf91e4e)
let bottomColor:UIColor = UIColor(hex: 0xcc4d7f)
gradientLayer.colors = [bottomColor.cgColor, topColor.cgColor]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
gradientLayer.frame = CGRect(x: 0, y: -20, width: navigationBar.frame.size.width, height: navigationBar.frame.size.height + 20)
navigationBar.layer.insertSublayer(gradientLayer, at: 0)
UIApplication.shared.statusBarStyle = .lightContent
}
}
它在root viewControllers中运行良好。
但导航栏的标题并没有详细显示VC。
到目前为止,我已尝试过这些解决方案,但这些解决方案都不起作用;
navigationItem.title = "Test"
2:
self.title = "Test"
3:
navigationController?.navigationBar.topItem?.title = "TEST" // This changes first viewController’s title
4:
navigationController?.navigationItem.title = "TEST"