Isaac Wasserman
Isaac Wasserman

Reputation: 1541

Navigation Bar gone

How can I present a view controller from my AppDelegate and have a Navigation bar added to that view with a back button to the previous view? I need to do this programmatically from my AppDelegate. Currently I can push a controller from there, but it doesn't act like a segue. It doesn't add a nav bar with a back button. Now I know I should be able to add one myself, but when I do it gets hidden. Currently I'm using pushViewController(), but I imagine that's not the best way to do it.

Upvotes: 0

Views: 189

Answers (2)

Adam Stoller
Adam Stoller

Reputation: 933

I had something that I think is similar, if not the same:

HIGH LEVEL VIEW

    The general composition of my App (thus far, and specific to the issue at hand - note: details about classes provided for context, not required for resolution) is as follows:
    • UIViewController (ViewController.swift) embedded in a UINavigationController
    • Buttons on UIViewController segue to a view with a custom class:
      • ExistingLocationViewController - subclass of:
        • UITableViewController
    • One of the buttons (Add New Location) in the UINavigationController's Toolbar segues to view with another custom class:
      • NewLocationViewController - subclass of:
        • UIViewController
        • CLLocationManagerDelegate
        • UITextFieldDelegate
    • There are a number of other items here, but I believe the above is sufficient as the foundation for the issue at hand

RESOLUTION

    In order to preserve the navigation-bar (and tool-bar) going both forward and back - I have the following code in my custom classes (note: the following is Swift-3 code, you may have to adjust for Swift-2):

    override func viewDidLoad() {
        super.viewDidLoad()
        //...
        navigationController?.isNavigationBarHidden = false
        navigationController?.isToolbarHidden       = false 
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated) // #=# not sure if this is needed
        navigationController?.isNavigationBarHidden = false
        navigationController?.isToolbarHidden       = false
    }
    

    You could actually omit the last two lines in viewWillDisappear, or perhaps even omit the entire override function
    The net result (for me) was as depicted below:


Hope that helps.

Upvotes: 1

Leo
Leo

Reputation: 24714

If you want add a NavigationController in appDelegate you can do it like this,in this way,your viewcontroller is load from storyboard

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let vc = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("vc") as! ViewController
    let nav = UINavigationController(rootViewController: vc)
    self.window?.rootViewController = nav
    self.window?.backgroundColor = UIColor.whiteColor()
    self.window?.makeKeyAndVisible()
    return true
}

Upvotes: 0

Related Questions