0

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?

Am1rFT
  • 227
  • 5
  • 18

2 Answers2

1

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)
ValW
  • 353
  • 1
  • 11
  • the red color which is in photo, i mean that – Am1rFT Sep 05 '18 at 12:00
  • so sorry to contact u again but I recently found out that the navigation title is not visible! what am I missing here? even I have defined my title in both my class and in the storyboard. – Am1rFT Sep 10 '18 at 10:01
  • hey no problem there is already a question regarding this: https://stackoverflow.com/questions/25167458/changing-navigation-title-programmatically hope this resolves your problem – ValW Sep 10 '18 at 14:21
0

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 !!

AlbinMrngStar
  • 289
  • 2
  • 13
  • my problem is not with saving the token and the conditions!! my problem is when the user goes to part 3 I want my UI has that red color in part 3 – Am1rFT Sep 05 '18 at 12:08
  • should I define the last change that you made in first navigation controller or in the first viewcontroller? – Am1rFT Sep 05 '18 at 12:19
  • implement it before navigating to the VC you want in red, so you will get the color – AlbinMrngStar Sep 05 '18 at 12:21
  • you mean I should put it in PART1 ? – Am1rFT Sep 05 '18 at 12:23
  • check the code now ! I have given u two ways to implement the color. One set's for whole app.-> use this in appDelegate And other for specific Navigation Controller(the one which need's unique color from default)-> use this inside the VC which you need red color – AlbinMrngStar Sep 05 '18 at 12:30