以编程方式在Swift 3中的两个UIViewControllers之间导航

问题描述:

在我的应用程序中有两个UIViewControllers(第一,用户选择字符,第二,其中字符信息显示)。 尝试从代码做所有事情。所以,我的代码:以编程方式在Swift 3中的两个UIViewControllers之间导航

AppDelegate.swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    self.window = UIWindow(frame: UIScreen.main.bounds) 
    guard let window = self.window else { 
     return false 
    } 
    UIApplication.shared.statusBarStyle = .lightContent 

    let viewController = ViewController() 
    let navigationController = UINavigationController(rootViewController: viewController) 

    window.rootViewController = navigationController 
    window.makeKeyAndVisible() 

    return true 
} 

的ViewController:UIViewController中

... 
    func buttonPressed(sender: UIButton) { 
     guard let typeName = sender.title(for: .normal), let unitType = UnitType(rawValue: typeName) else { 
      return 
     } 
     let unitViewController = UnitViewController(unitType: unitType) 
     let navigationController = UINavigationController(rootViewController: unitViewController) 
     self.present(navigationController, animated: true, completion: nil) 
    } 

而且UnitViewController:UIViewController中(其中选定的字符的信息显示):

... 
    fileprivate func setupNavigationBar() { 
     let backButton = UIBarButtonItem(title: "<", style: .plain, target: self, action: #selector(backToViewController(sender:))) 
     self.navigationItem.setLeftBarButton(backButton, animated: true) 
     self.navigationItem.titleView?.backgroundColor = AppColor.Black.color 
     self.navigationItem.titleView?.tintColor = .white 
     self.navigationItem.title = "\(self.unitType.rawValue)" 
    } 

    func backToViewController(sender: AnyObject) { 
     self.dismiss(animated: true) { 
      let viewController = ViewController() 
      let navigationController = UINavigationController(rootViewController: viewController) 
      self.present(navigationController, animated: true, completion: nil) 
     } 
    } 

所以我有两个屏幕:

enter image description here enter image description here

嗯,我有一些问题: 1.获取

2016年12月2日05:48:21.855的API中唤醒[16402:551729]警告:尝试 在窗口层次上显示在 的视图不是 !在UnitViewController中按'后退'按钮时出现警告。我做错了什么?

  1. 导航栏的灰色背景颜色和黑色。如何改变它为黑色和白色?

  2. 如何获得UIBarButtonItem的'系统'返回按钮,而不仅仅是标题为“<”?所以我UnitViewController导航栏应该看起来像这样: enter image description here

任何帮助将不胜感激!

更新1: 1.警告了,这要归功于@dip 2.制造导航栏暗,感谢@ aznelite89。 但是,有我在AppDelegate.swift代码:

UINavigationBar.appearance().barTintColor = AppColor.Black.color 

AppColor.Black是我使用的视图控制器背景完全相同的颜色,而是它如何现在看起来:

enter image description here

貌似导航栏的字母是不是1.0?

更新2: 导航栏的颜色和颜色我已经不同之间在RGB值中使用13,所以我已经黑了它设置NavigationBar颜色的RGB值减少13比原来的颜色...这没关系,现在

+1

它不是ne每次都改变根视图控制器。 而不是这个 让unitViewController = UnitViewController(UNITTYPE:UNITTYPE) 让navigationController =的UINavigationController(RootViewController的:unitViewController) self.present(navigationController,动画:真,完成:无) 你可以做 让利unitViewController = UnitViewController(unitType:unitType) self.navigationController?.pushViewController(unitViewController,animated:true) – dip

+0

谢谢,它的工作 – zzheads

  1. 试试这个: -

    let viewController = ViewController() 
    viewController.modalPresentationStyle = .overCurrentContext 
    window?.rootViewController?.present(viewController, animated: true, completion: nil) 
    
  2. 在应用delegate.swift补充一点: -

    UINavigationBar.appearance().barTintColor = UIColor.black 
    
  3. 你可能在你的故事板属性检查

    更改这些属性
+0

2. UINavigationBar.appearance()。barTintColor = UIColor.black作品3.不,我已经摆脱Main.storyboard和LaunchScreen中的更改。故事板没有任何影响 – zzheads

+0

奇怪的事情设置UINavigationBar.appearance()。barTintColor到.black并没有使NavigationBar黑色,它有黑色,但颜色较浅(与主视图不同) – zzheads

+0

启动screen.storyboard只影响启动屏幕 – aznelite89