我在 swift 中的导航控制器代码有问题

problem with my Navigation Controller code in swift

我现在在我的应用程序中有 5 个视图控制器用于我的 5 个选项卡栏项目。每个视图控制器都具有相同的样式,只是标题不同。

这是我在所有这些视图控制器中单独使用并在 viewDidLoad() 中调用该函数的函数

 private func setupNavBar() {
    let navBar = navigationController?.navigationBar
    navBar?.prefersLargeTitles = true
    navigationItem.title = "Library"
    navigationItem.rightBarButtonItem?.tintColor = .white
    navBar?.backgroundColor = UIColor.rgb(red: 10, green: 10, blue: 10)
    navBar?.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    navBar?.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

    navBar?.setBackgroundImage(UIImage(), for: .default)
    navBar?.shadowImage = UIImage()
}

我不想在每个 VC 都这样做。相反,我想调用如下函数

setupNavBar(with largeTitles: Bool, has title: String)

我尝试创建一个新文件 Functions.swift 并将代码添加为一个函数,但我得到的错误是 navigationContoller、navigationBar、navigationItem as undefined

class Functions: NSObject {

    public static func setupNavBar(largeTitles: Bool, title: String, viewController: UIViewController?) {
        guard let viewController = viewController else { return }
        viewController.navigationController?.navigationBar.prefersLargeTitles = largeTitles
        viewController.navigationController?.navigationItem.title = title.capitalized
        /* ... */
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        Functions.setupNavBar(largeTitles: false, title: "Library", viewController: self)
    }
}

Use this code in the app delegate and the code is applied to all ViewControllers.You have to edit the properties accordingly to your requirements from the code:

UINavigationBar.appearance().barTintColor = UIColor(red: 240/255, green: 224/255, blue: 227/255, alpha: 1)
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
   UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Cardo-Regular", size: 22)!]
   UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Cardo-Regular", size: 20)!], for: UIControlState.normal)

您可以像这样在 UIViewController 上创建扩展:

extension UIViewController {
    public func setupNavBar(with largeTitles: Bool, has title: String) {
        let navBar = navigationController?.navigationBar
        navBar?.prefersLargeTitles = largeTitles
        navigationItem.title = title
        navigationItem.rightBarButtonItem?.tintColor = .white
        navBar?.backgroundColor = UIColor(red: 10/255, green: 10/255, blue: 10/255, alpha: 1)
        navBar?.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        navBar?.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

        navBar?.setBackgroundImage(UIImage(), for: .default)
        navBar?.shadowImage = UIImage()
    }
}

然后您可以在任何视图控制器中像这样调用它:

self.setupNavBar(with: false, has: "Check")

我倾向于使用 UIViewController 扩展,因为您实际上是在设置视图控制器本身的样式,因此可以向其中添加各种其他设置代码。