簡體   English   中英

我的導航控制器代碼出現問題

[英]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 為 undefined

你可以像這樣在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擴展,因為您實際上是在設置視圖控制器本身的樣式,因此可以向其中添加各種其他設置代碼。

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)
    }
}

在應用程序委托中使用此代碼,該代碼將應用於所有 ViewControllers。您必須根據代碼中的要求相應地編輯屬性:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM