1
votes

hello I have ControllerA in which I am hiding a NavigationBar. On the ControllerB I am showing the NavigationBar. On the ControllerB I have implemented a searchBar with TableView. So when user selects any row I am dismissing the controller. Problem is It shows navigationBar on controllerA.

This is how I am first hiding the NavigationBar in ControllerA

ControllerA

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

     self.navigationController?.setNavigationBarHidden(true, animated: true)

}

ControllerB here I am showing it

 override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated);
            self.navigationController?.setNavigationBarHidden(false, animated: true)
        }

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

 self.navigationController!.popViewControllerAnimated(true)

}

What I have tried is I try to put hidden navigation Code in viewWillDisappear function in ControllerB but it doesn't work. It hides the bar in some delay after showing it. I also tried to put in the viewDidLoad function of ControllerA, But still it shows the bar

Please tell me how can I hide the navigationBar on ControllerA after ControllerB get dismissed

5
set hidden navigation bar in viewDidDisappear in controller B?Sonic Master
It didn't work. still showinguser1hjgjhgjhggjhg
hide the navigation bar in viewDidAppear in controller A.PGDev
PGDev's answer is what you are looking for.Sonic Master
@PGDev well it removes but after some delay. it shows for 1 2 seconduser1hjgjhgjhggjhg

5 Answers

3
votes

you can use notification post a notification on controllerB in view will Diss Appear and recive it in ControllerA .and at ControllerA in the selector method of notification write code to hide navigationbar .

write this code in view will disappear in controllerB-

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifierForHideNavigationBar", object: nil)

write this code in controller

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotificationHideBar:", name:"NotificationIdentifierForHideNavigationBar", object: nil)

Use these method:-

   func methodOfReceivedNotificationHideBar(notification: NSNotification){
        self.navigationController?.setNavigationBarHidden(true, animated: true)     
  }

try this as written

2
votes

In the first view controller viewWillAppear(), add this:

override func viewWillAppear(animated: Bool) {

 self.navigationController?.navigationBarHidden = true
 }

In the second one, add this:

 override func viewWillAppear(animated: Bool) {

      self.navigationController?.navigationBarHidden = false
 }




override func viewDidDisappear(animated: Bool) {

     self.navigationController?.navigationBarHidden = true
}
0
votes

Add the below code in ControllerA

Code:

override func viewWillAppear(animated: Bool) {
     self.navigationController?.setNavigationBarHidden(true, animated: true)
    self.view.layoutIfNeeded()
}
override func viewWillDisappear(animated: Bool) {
    self.navigationController?.setNavigationBarHidden(false, animated: true)
    self.view.layoutIfNeeded()
}

It is working fine for me... Try it ...!!!

0
votes

Its so simple
In first view controller viewWillAppear() add code

override func viewWillAppear(animated: Bool) {
     self.navigationController?.setNavigationBarHidden(true, animated: true);
}


In second view controller add following code

override func viewWillAppear(animated: Bool) {
    self.navigationController?.setNavigationBarHidden(false, animated: true);
}

override func viewDidDisappear(animated: Bool) {
    self.navigationController?.setNavigationBarHidden(true, animated: true);
}

OutPut Result: FirstViewcontroller without navigation bar
enter image description here
Second view controller with navigation bar
enter image description here

0
votes

Is this entiere code? Beacues I use those two ViewControllers

ViewControllerA

class ViewControllerA: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
}

TableViewControllerA

class TableViewControllerA: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

        cell.textLabel?.text = "test"

        return cell
    }


    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.navigationController?.popViewControllerAnimated(true)
    }
}

This is how it look like. YT