简体   繁体   English

使用快速操作时导航栏丢失

[英]Navigation Bar is missing when using Quick Actions

I am using quick actions for my app and they are working correctly, except that the Navigation Bar is missing (no back button). 我正在为我的应用程序使用快速操作,并且它们正常运行,但缺少导航栏(没有后退按钮)。 Here is my code: 这是我的代码:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    let vc = storyboard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController

    switch shortcutItem.type {
    case "AddIncome":
        vc.type = .income
        app?.mainVC.dismiss(animated: false, completion: nil)
        app?.mainVC.present(vc, animated: true, completion: nil)
    case "AddExpense":
        vc.type = .expense
        app?.mainVC.dismiss(animated: false, completion: nil)
        app?.mainVC.present(vc, animated: true, completion: nil)
    default:
        break;
    }
}

mainVC is actually the view controller from where I present the AddViewController , the vc where my navigation bar is missing. mainVC实际上是我呈现AddViewController的视图控制器,缺少导航栏的vc。

I can't seem to see what the issue is. 我似乎看不到问题所在。 Do I have to do some additional stuff to make it work correctly ? 我是否需要做一些其他工作才能使其正常工作?

@Kobe You need to add a navigation controller to show navigation bar when you presenting any view controller. @Kobe在呈现任何视图控制器时,需要添加导航控制器以显示导航栏。 Just try below code. 只需尝试下面的代码。

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    let vc = storyboard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController
    var nav = UINavigationController()
    nav.viewControllers = [vc]
    switch shortcutItem.type {
    case "AddIncome":
        vc.type = .income
        app?.mainVC.dismiss(animated: false, completion: nil)
        app?.mainVC.present(nav, animated: true, completion: nil)
    case "AddExpense":
        vc.type = .expense
        app?.mainVC.dismiss(animated: false, completion: nil)
        app?.mainVC.present(nav, animated: true, completion: nil)
    default:
        break;
    }
}

Now you can add back button on AddViewController. 现在,您可以在AddViewController上添加返回按钮。

override func viewDidLoad() {
    super.viewDidLoad()
    let button = UIBarButtonItem(title: "Back", style:.plain, target: self, action: #selector(self.moveToPreviousScreen))
    self.navigationItem.backBarButtonItem = button
}

Hope it will help you thanks. 希望对您有所帮助。

There are a lot of points you can consider: 您可以考虑很多方面:

  1. Have you added a Navigation Controller. 您是否添加了导航控制器。 (I know its too basic but some forget) (我知道它太基本了,但有些人忘记了)
  2. Have you set your view controller as the root for the navigation controller? 您是否已将视图控制器设置为导航控制器的根目录?
  3. You can try setting the navigation bar programmatically. 您可以尝试以编程方式设置导航栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM