Why does popping UIViewController change navigationItem's titleTextAttributes?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm building a social network that allows users to navigate from one profile to the next, by viewing who they follow. For simplicity, I created a test project with two view controllers: ViewController and SecondViewController.



Each view controller has a button that fires an IBAction to instantiate the next view controller. That view is then pushed on the navigation stack. A user can do this as long as there is another viewController to push. But when they start returning/popping is when I have issues.



Here's ViewController:



class ViewController: UIViewController {
@IBOutlet weak var pageNumberLabel: UILabel!

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.navigationItem.title = "First"
setUp()
}

func setUp() {
print("Setup 1")

let navBarAppearance = self.navigationController!.navigationBar
navBarAppearance.setBackgroundImage(UIImage(), for: .default)
navBarAppearance.shadowImage = UIImage()
navBarAppearance.barStyle = .black
navBarAppearance.isTranslucent = true
navBarAppearance.tintColor = .white
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
self.navigationController?.view.backgroundColor = UIColor.lightGray
}

@IBAction func pushSecondViewController() {
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
print("-")
self.navigationController?.pushViewController(vc, animated: true)
}
}
}


Here's SecondViewController:



class SecondViewController: UIViewController {
@IBOutlet weak var pageNumberLabel: UILabel!

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.navigationItem.title = "Second"
setUp()
}

func setUp() {
print("Setup 2")

let navBarAppearance = self.navigationController!.navigationBar
navBarAppearance.isTranslucent = false
navBarAppearance.barTintColor = .white
navBarAppearance.tintColor = .blue
navBarAppearance.barStyle = .black
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
}

@IBAction func pushFirstViewController() {
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "firstVC") as? ViewController {
print("-")
self.navigationController?.pushViewController(vc, animated: true)
}
}
}


When SecondViewController is popped and ViewController is presented, the navigationTitle stays UIColor.blue. However, if I swipe from SecondViewController to ViewController, the title correctly changes colors.



Why is this?










share|improve this question































    0















    I'm building a social network that allows users to navigate from one profile to the next, by viewing who they follow. For simplicity, I created a test project with two view controllers: ViewController and SecondViewController.



    Each view controller has a button that fires an IBAction to instantiate the next view controller. That view is then pushed on the navigation stack. A user can do this as long as there is another viewController to push. But when they start returning/popping is when I have issues.



    Here's ViewController:



    class ViewController: UIViewController {
    @IBOutlet weak var pageNumberLabel: UILabel!

    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationItem.title = "First"
    setUp()
    }

    func setUp() {
    print("Setup 1")

    let navBarAppearance = self.navigationController!.navigationBar
    navBarAppearance.setBackgroundImage(UIImage(), for: .default)
    navBarAppearance.shadowImage = UIImage()
    navBarAppearance.barStyle = .black
    navBarAppearance.isTranslucent = true
    navBarAppearance.tintColor = .white
    navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
    self.navigationController?.view.backgroundColor = UIColor.lightGray
    }

    @IBAction func pushSecondViewController() {
    if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
    print("-")
    self.navigationController?.pushViewController(vc, animated: true)
    }
    }
    }


    Here's SecondViewController:



    class SecondViewController: UIViewController {
    @IBOutlet weak var pageNumberLabel: UILabel!

    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationItem.title = "Second"
    setUp()
    }

    func setUp() {
    print("Setup 2")

    let navBarAppearance = self.navigationController!.navigationBar
    navBarAppearance.isTranslucent = false
    navBarAppearance.barTintColor = .white
    navBarAppearance.tintColor = .blue
    navBarAppearance.barStyle = .black
    navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
    }

    @IBAction func pushFirstViewController() {
    if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "firstVC") as? ViewController {
    print("-")
    self.navigationController?.pushViewController(vc, animated: true)
    }
    }
    }


    When SecondViewController is popped and ViewController is presented, the navigationTitle stays UIColor.blue. However, if I swipe from SecondViewController to ViewController, the title correctly changes colors.



    Why is this?










    share|improve this question



























      0












      0








      0


      1






      I'm building a social network that allows users to navigate from one profile to the next, by viewing who they follow. For simplicity, I created a test project with two view controllers: ViewController and SecondViewController.



      Each view controller has a button that fires an IBAction to instantiate the next view controller. That view is then pushed on the navigation stack. A user can do this as long as there is another viewController to push. But when they start returning/popping is when I have issues.



      Here's ViewController:



      class ViewController: UIViewController {
      @IBOutlet weak var pageNumberLabel: UILabel!

      override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)

      self.navigationItem.title = "First"
      setUp()
      }

      func setUp() {
      print("Setup 1")

      let navBarAppearance = self.navigationController!.navigationBar
      navBarAppearance.setBackgroundImage(UIImage(), for: .default)
      navBarAppearance.shadowImage = UIImage()
      navBarAppearance.barStyle = .black
      navBarAppearance.isTranslucent = true
      navBarAppearance.tintColor = .white
      navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
      self.navigationController?.view.backgroundColor = UIColor.lightGray
      }

      @IBAction func pushSecondViewController() {
      if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
      print("-")
      self.navigationController?.pushViewController(vc, animated: true)
      }
      }
      }


      Here's SecondViewController:



      class SecondViewController: UIViewController {
      @IBOutlet weak var pageNumberLabel: UILabel!

      override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)

      self.navigationItem.title = "Second"
      setUp()
      }

      func setUp() {
      print("Setup 2")

      let navBarAppearance = self.navigationController!.navigationBar
      navBarAppearance.isTranslucent = false
      navBarAppearance.barTintColor = .white
      navBarAppearance.tintColor = .blue
      navBarAppearance.barStyle = .black
      navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
      }

      @IBAction func pushFirstViewController() {
      if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "firstVC") as? ViewController {
      print("-")
      self.navigationController?.pushViewController(vc, animated: true)
      }
      }
      }


      When SecondViewController is popped and ViewController is presented, the navigationTitle stays UIColor.blue. However, if I swipe from SecondViewController to ViewController, the title correctly changes colors.



      Why is this?










      share|improve this question
















      I'm building a social network that allows users to navigate from one profile to the next, by viewing who they follow. For simplicity, I created a test project with two view controllers: ViewController and SecondViewController.



      Each view controller has a button that fires an IBAction to instantiate the next view controller. That view is then pushed on the navigation stack. A user can do this as long as there is another viewController to push. But when they start returning/popping is when I have issues.



      Here's ViewController:



      class ViewController: UIViewController {
      @IBOutlet weak var pageNumberLabel: UILabel!

      override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)

      self.navigationItem.title = "First"
      setUp()
      }

      func setUp() {
      print("Setup 1")

      let navBarAppearance = self.navigationController!.navigationBar
      navBarAppearance.setBackgroundImage(UIImage(), for: .default)
      navBarAppearance.shadowImage = UIImage()
      navBarAppearance.barStyle = .black
      navBarAppearance.isTranslucent = true
      navBarAppearance.tintColor = .white
      navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
      self.navigationController?.view.backgroundColor = UIColor.lightGray
      }

      @IBAction func pushSecondViewController() {
      if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
      print("-")
      self.navigationController?.pushViewController(vc, animated: true)
      }
      }
      }


      Here's SecondViewController:



      class SecondViewController: UIViewController {
      @IBOutlet weak var pageNumberLabel: UILabel!

      override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)

      self.navigationItem.title = "Second"
      setUp()
      }

      func setUp() {
      print("Setup 2")

      let navBarAppearance = self.navigationController!.navigationBar
      navBarAppearance.isTranslucent = false
      navBarAppearance.barTintColor = .white
      navBarAppearance.tintColor = .blue
      navBarAppearance.barStyle = .black
      navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
      }

      @IBAction func pushFirstViewController() {
      if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "firstVC") as? ViewController {
      print("-")
      self.navigationController?.pushViewController(vc, animated: true)
      }
      }
      }


      When SecondViewController is popped and ViewController is presented, the navigationTitle stays UIColor.blue. However, if I swipe from SecondViewController to ViewController, the title correctly changes colors.



      Why is this?







      ios swift uinavigationcontroller uinavigationbar uinavigationitem






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 19:01







      Richard Poutier

















      asked Nov 16 '18 at 18:32









      Richard PoutierRichard Poutier

      439




      439
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I found a workaround solution to this issue...originally posted here. I put the following code in my setUp() function for my navigation bar.



          let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
          titleView.backgroundColor = UIColor.clear
          let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
          titleLabel.textAlignment = .center
          titleLabel.text = [My View Name]
          titleLabel.textColor = .blue
          titleView.addSubview(titleLabel)
          self.navigationItem.titleView = titleView
          self.navigationItem.title = [My View Name]





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343525%2fwhy-does-popping-uiviewcontroller-change-navigationitems-titletextattributes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I found a workaround solution to this issue...originally posted here. I put the following code in my setUp() function for my navigation bar.



            let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
            titleView.backgroundColor = UIColor.clear
            let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
            titleLabel.textAlignment = .center
            titleLabel.text = [My View Name]
            titleLabel.textColor = .blue
            titleView.addSubview(titleLabel)
            self.navigationItem.titleView = titleView
            self.navigationItem.title = [My View Name]





            share|improve this answer




























              0














              I found a workaround solution to this issue...originally posted here. I put the following code in my setUp() function for my navigation bar.



              let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
              titleView.backgroundColor = UIColor.clear
              let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
              titleLabel.textAlignment = .center
              titleLabel.text = [My View Name]
              titleLabel.textColor = .blue
              titleView.addSubview(titleLabel)
              self.navigationItem.titleView = titleView
              self.navigationItem.title = [My View Name]





              share|improve this answer


























                0












                0








                0







                I found a workaround solution to this issue...originally posted here. I put the following code in my setUp() function for my navigation bar.



                let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
                titleView.backgroundColor = UIColor.clear
                let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
                titleLabel.textAlignment = .center
                titleLabel.text = [My View Name]
                titleLabel.textColor = .blue
                titleView.addSubview(titleLabel)
                self.navigationItem.titleView = titleView
                self.navigationItem.title = [My View Name]





                share|improve this answer













                I found a workaround solution to this issue...originally posted here. I put the following code in my setUp() function for my navigation bar.



                let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
                titleView.backgroundColor = UIColor.clear
                let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
                titleLabel.textAlignment = .center
                titleLabel.text = [My View Name]
                titleLabel.textColor = .blue
                titleView.addSubview(titleLabel)
                self.navigationItem.titleView = titleView
                self.navigationItem.title = [My View Name]






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 19:33









                Richard PoutierRichard Poutier

                439




                439
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343525%2fwhy-does-popping-uiviewcontroller-change-navigationitems-titletextattributes%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly