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

时间:2016-12-02 02:56:31

标签: swift uinavigationcontroller uinavigationbar uinavigationitem

在我的应用程序中有两个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-02 05:48:21.855 API Awakens [16402:551729]警告:尝试   介绍    谁的观点不是   在窗口层次结构中!   在UnitViewController中按“后退”按钮时发出警告。我做错了什么?

  1. NavigationBar的灰色背景颜色和黑色。如何将其改为黑白?

  2. 如何获取UIBarButtonItem的'system'后退按钮,而不仅仅是.plain标题为“&lt;”?所以我的UnitViewController导航栏应如下所示: enter image description here

  3. 任何帮助将不胜感激!

    更新1: 警告消失了,感谢@dip 2.感谢@ aznelite89,导航栏变暗。 但是,AppDelegate.swift中有我的代码:

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

    AppColor.Black与我在ViewController中用于背景的颜色完全相同,但它现在看起来如何:

    enter image description here

    看起来NavigationBar的alpha不是1.0 ......

    更新2: NavigationBar的颜色与我使用的颜色之间的差异在RGB值中是13,所以我已经将它设置为导航栏的颜色,其RGB值比原始颜色少13 ...现在可以了

1 个答案:

答案 0 :(得分:1)

  1. 试试这个: -

    let viewController = ViewController()
    viewController.modalPresentationStyle = .overCurrentContext
    window?.rootViewController?.present(viewController, animated: true, completion: nil)
    
  2. 在app delegate.swift中添加: -

    UINavigationBar.appearance().barTintColor = UIColor.black
    
  3. 您可以在故事板属性检查器

  4. 上更改这些属性