簡體   English   中英

swift - UINavigationController擴展無法正常工作

[英]swift - UINavigationController extension not working

我將在不同的視圖控制器中的所有導航控制器上使用類似的屬性,除了一些導航項和顯然標題。

我做了一個擴展計划,可以調用它並設置我的默認值; 但是,它沒有做任何事情。 當我只是將它放在實際的類中而不是在我調用SetDefaults時,代碼確實有效。

擴展

extension UINavigationController {
    func setDefaults(titleText: String){
        let appDel = UIApplication.sharedApplication().delegate as! AppDelegate

        //Set title label
        let label = UILabel()
        label.font = UIFont(name: appDel.regularDefaultFont, size: 16)
        label.text = titleText
        label.kern(1.0)
        label.sizeToFit()
        self.navigationItem.titleView = label

        //Set white background tint color
        self.navigationBar.barTintColor = UIColor.whiteColor()

        //Set navigation bar bottom line
        let bottomBorderRect = CGRect(x: 0, y: UIScreen.mainScreen().bounds.height, width: UIScreen.mainScreen().bounds.width, height: 1)
        let bottomBorderView = UIView(frame: bottomBorderRect)
        bottomBorderView.backgroundColor = UIColor(r: 250, g: 250, b: 250)
        self.navigationBar.addSubview(bottomBorderView)
    }
}

使用navigationcontroller從類調用SetDefaults:

self.navigationController.SetDefaults("Login")

在你的故事板,你應該選擇Segue類型show ,同時連接一個視圖控制器到另一個或請選擇您賽格瑞>在屬性檢查器>故事書賽格瑞>樣>顯示(例如)推。 如圖所示

在此輸入圖像描述

它將永久解決問題:)

你的代碼應該是這樣的。

let vcLogin = loginViewController(
            nibName: "loginViewController",
            bundle: nil)
        navigationController?.pushViewController(vcLogin,
            animated: true )

希望這就像水流一樣......

暫無
暫無

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

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