获取UIViewController.title以在模态呈现的UINavigationController中显示

时间:2016-05-13 16:10:17

标签: ios uinavigationcontroller uinavigationbar uinavigationitem

我知道这个问题会被问到很多。

我已经尝试过每一个答案(我认为)并且无法显示出来的标题。

我有一个静态方法,用于以模态方式显示视图控制器。

我想我理解导航控制器从它所呈现的视图控制器的title属性获取其标题,但我无法显示我的vc标题,而我尝试的其他方法不是工作,或者。

static func present(vc vc: UIViewController) {

  // The navCtrl presents a vc, and uses that title.
  // I should be able to set this, right?
  vc.title = "vc.title" // doesn't show up
  vc.navigationItem.title = "vc.navigationItem.title" // nothing
  vc.navigationItem.prompt = "vc.navigationItem.prompt" // doesn't show
  vc.navigationController?.title = "vc.navigationController?.title" // nuttin'

  let navCtrl = UINavigationController(rootViewController: vc)
  navCtrl.navigationBar.tintColor = UIColor.whiteColor()
  navCtrl.navigationBar.barTintColor = UIColor.blackColor()
  navCtrl.navigationBar.translucent = false

  // How about adding a UINavigationItem? Nope.
  // let item = UINavigationItem(title: "UINavigationItem")
  // navCtrl.navigationBar.pushNavigationItem(item, animated: true) // CRASH: Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.

  // Can I set something on the navCtrl? Huh-uh. None of this works
  navCtrl.navigationController?.title = "navCtrl.navigationController?.title"
  navCtrl.navigationItem.prompt = "navCtrl.navigationItem.prompt"
  navCtrl.title = "navCtrl.title"
  navCtrl.navigationItem.title = "navCtrl.navigationItem.title"

  UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(navCtrl, animated: true, completion: nil)
}

无论我尝试过什么,我只需要一个带有关闭按钮的空导航栏:

enter image description here

0 个答案:

没有答案