Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation and tab bar missing when presenting view controller

I am implementing home screen shortcuts using 3D Touch, and it's working well, however the way I currently have it means that when the shortcut takes the user to a specific view controller, the tab bar and navigation bar is missing.

This is my code:

func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
    var handled = false

    if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) {
        let rootViewController = window!.rootViewController

        switch shortcutType {
        case .Favourites:
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let rootController = storyboard.instantiateViewControllerWithIdentifier("favourites") as! FavouritesTableViewController
            rootController.parkPassed = DataManager.sharedInstance.getParkByName(NSUserDefaults.standardUserDefaults().stringForKey("currentPark")!)
            self.window?.rootViewController = rootController
            self.window?.makeKeyAndVisible()

            handled = true
        }
    return handled
}

Can anyone suggest what I need to change in the code?

This is the starboard layout (FavouritesTableViewController is indicated):

enter image description here

EDIT:

Here is my updated code:

@available(iOS 9.0, *)
func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
    var handled = false

    if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) {
        switch shortcutType {
        case .Favourites:
            print("favourites")
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let rootController = storyboard.instantiateViewControllerWithIdentifier("favourites") as! FavouritesViewController
            rootController.parkPassed = DataManager.sharedInstance.getParkByName(NSUserDefaults.standardUserDefaults().stringForKey("currentPark")!)

            let root = UIApplication.sharedApplication().delegate as! AppDelegate
            if let navCont = root.window?.rootViewController?.navigationController {
                navCont.presentViewController(rootController, animated: true, completion: nil)
            } else {
                root.window?.rootViewController?.presentViewController(rootController, animated: true, completion: nil)
            }

            root.window?.makeKeyAndVisible()

            handled = true
        }
    }
    return handled
}
like image 407
user3746428 Avatar asked Sep 28 '15 16:09

user3746428


1 Answers

Try this:

Get the delegate from app delegate:

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

GEt the controller you would like to to present from storyboard:

Controller *cont=//get the reference from storyboard either using storyboard ID

and then make your rootview present the other controller:

[appDelegate.window.rootViewController presentViewController:cont animated:YES completion:^{
                            DLog(@"presented your view ON TOP of the  tab bar controller");
                        }];

Swift:

  var appDelegate: AppDelegate = UIApplication.sharedApplication().delegate()
  var cont: Controller = //get the reference form storyboard
  appDelegate.window.rootViewController.presentViewController(cont, animated: true, completion: {    DLog("presented your view ON TOP of the  tab bar controller")

 })

you can move the presenting stuff on main thread,if you like!!!!

like image 58
Teja Nandamuri Avatar answered Oct 09 '22 22:10

Teja Nandamuri