【问题标题】:sending ui data in swift快速发送 ui 数据
【发布时间】:2019-02-10 13:06:31
【问题描述】:

我是 SWIFT 的初学者,现在我遇到了 UI 问题。在这个PHOTO 中,我展示了我的用户界面来澄清我在说什么。在第 1 部分中,我检查用户是否登录到他的帐户,如果是,则进入第 3 部分,如果没有,则进入第 2 部分。当用户在第 2 部分登录时,我将用户转移到第 3 部分。 第 1 部分和第 2 部分不应有任何导航颜色,但第 3 部分应具有导航颜色。

第 1 部分:

if let token = UserDefaults.standard.string(forKey: ConstantsKey.token){
        if !token.isEmpty{
            let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
            let rootController = UINavigationController(rootViewController: vc)
            self.present(rootController, animated: true, completion: nil)
        }else{
            let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "LoginVc")
            let rootController = UINavigationController(rootViewController: vc)
            self.present(rootController, animated: true, completion: nil)
        }
    }else{
        let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "LoginVc")
        let rootController = UINavigationController(rootViewController: vc)
        self.present(rootController, animated: true, completion: nil)
    }

第 2 部分:

let storyboard : UIStoryboard = UIStoryboard(name: "MainTabBar", bundle: nil)
                        let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
                        let rootController = UINavigationController(rootViewController: vc)
                        self.present(rootController, animated: true, completion: nil)

我想在第 3 部分中使用那种红色!但是每当我运行应用程序时,都会显示导航控制器的默认颜色

有人知道我应该如何管理/处理这个问题吗?

【问题讨论】:

    标签: ios iphone swift user-interface uinavigationcontroller


    【解决方案1】:

    登录集时

    UserDefaults.standard.set(true,forKey: ConstantsKey.token)
    

    并在路由页面检查这个

    if UserDefault.standar.bool(forKey: ConstantsKey.token){
       //Go to home page
    }else {
       setUpNav()
       //Go to login page
    }
    

    设置导航

    这个将为整个应用设置

        func setUpNav(){
                UINavigationBar.appearance().isTranslucent = false
                UINavigationBar.appearance().tintColor = UIColor.red
                UINavigationBar.appearance().barTintColor  = UIColor.white
    
    
                UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white,
                                                                    NSAttributedStringKey.font: UIFont(name:"Cairo-Bold", size:22.0)!]
            }
    

    如果你想要单个 VC,

    在那个 VC 里面写这个,

    确保 VC 的根目录是 navigationController

    self.navigationController.navigationBar.barTintColor = .white
    self.navigationController.navigationBar.tintColor = .red
    

    即使你没有设置任何值,它也不会崩溃!!

    【讨论】:

    • 我的问题不在于保存令牌和条件!!我的问题是当用户进入第 3 部分时,我希望我的 UI 在第 3 部分中具有红色
    • 我应该定义您在第一个导航控制器或第一个视图控制器中所做的最后更改吗?
    • 在导航到你想要的红色VC之前实现它,这样你就会得到颜色
    • 你的意思是我应该把它放在 PART1 中?
    • 现在检查代码!我给了你两种实现颜色的方法。一组用于整个应用程序。-> 在 appDelegate 中使用它,其他用于特定导航控制器(默认需要唯一颜色的那个)-> 在需要红色的 VC 中使用它
    【解决方案2】:

    然后在第 1 部分:

        if !token.isEmpty{
                    let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                    let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
                    let rootController = UINavigationController(rootViewController: vc)
                    rootController?.navigationBar.barTintColor = UIColor.red
                    self.present(rootController, animated: true, completion: nil)
    

    第 2 部分:

    let storyboard : UIStoryboard = UIStoryboard(name: "MainTabBar", bundle: nil)
                            let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
                            let rootController = UINavigationController(rootViewController: vc)
                            rootController?.navigationBar.barTintColor = UIColor.red
                            self.present(rootController, animated: true, completion: nil)
    

    【讨论】:

    • 照片中的红色,我的意思是
    • 很抱歉再次与您联系,但我最近发现导航标题不可见!我在这里想念什么?甚至我在课堂和故事板中都定义了我的标题。
    • 嘿没问题已经有一个关于这个的问题:stackoverflow.com/questions/25167458/…希望这能解决你的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多