繁体   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