Am1rFT
Am1rFT

Reputation: 247

sending ui data in swift

I'm a bit beginner in SWIFT and right now I'm facing a problem whit UI. In this PHOTO I'm showing my UI to clarify what I'm saying . in part 1 I check if the user is logged in to his account or not, if yes it goes to part 3, if not it goes to part 2. when user login in part 2, I transfer the user to part 3. part 1 and 2 should not have any navigation color, though the part 3 should have the navigation color.

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

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

I want to have that red color in part 3 ! but whenever I run the application in shows the defualt color of the navigation controller

does anybody knows how should I manage/handle this problem?

Upvotes: 0

Views: 83

Answers (2)

AlbinMrngStar
AlbinMrngStar

Reputation: 349

While logging in set

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

And in routing page check this

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

setting navigation

this one will set for whole app

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

if you want for single VC,

write this inside that VC,

make sure that VC's root is navigationController

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

Even if you haven't set any value, it doesn't crash !!

Upvotes: 0

ValW
ValW

Reputation: 363

Then in Part 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)

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

Upvotes: 1

Related Questions