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

标签 swift uinavigationcontroller uinavigationbar uinavigationitem

在我的应用程序中有两个 UIViewController(首先,用户选择角色,其次,显示角色信息)。 尝试从代码做所有事情。所以我的代码:

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
}

View Controller :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 The API Awakens[16402:551729] Warning: Attempt to present on whose view is not in the window hierarchy! warning when press 'back' button in UnitViewController. What I am doing wrong?

  1. NavigationBar 的灰色背景色和黑色。怎么改成黑白的?

  2. 如何获取 UIBarButtonItem 的“系统”后退按钮,而不仅仅是带有标题“<”的 .plain?所以我的 UnitViewController 导航栏应该是这样的: enter image description here

任何帮助将不胜感激!

更新 1: 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,所以我已经破解了它设置 NavigationBar 的颜色,RGB 值比原始颜色少 13...现在可以了

最佳答案

  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. 您可以在 Storyboard属性检查器上更改这些属性

关于swift - 以编程方式在 Swift 3 中的两个 UIViewController 之间导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40923994/

相关文章:

ios - 如何使用 NSUnderlineStyle.PatternDot

ios - 当我向 NavigationController 呈现 "whose view is not in the window hierarchy!"错误时

iphone - iOS 内存泄漏导致更改 View Controller 时崩溃

ios - 如何从多个 VC 更新 Container ViewController?

ios - 如何在 Swift 中为 UIImageView 对象分配一个 Action

iphone - 我如何识别 self.editButtonItem 按钮的单击事件?

ios - 使用带有半透明导航栏的 UISearchDisplayController 时变暗区域错位

ios - 导航栏背景颜色

python - Swift 2.0 中的类和对象级变量

ios - iPhone登录UINavigationController内的其他页面