Ios 导航栏是否显示为黑色?

Ios 导航栏是否显示为黑色?,ios,swift,uinavigationbar,Ios,Swift,Uinavigationbar,我正在为我的应用程序启动时设置新的导航。但当我启动时,它会出现在黑色动画中。在黑色动画之后,它会设置导航栏。请告诉我是什么问题 我正在使用下面的代码 var controller = UIViewController() //App Theming var navController = UINavigationController() navController.navigationBar.barTintColor = UIColor.white navController.navigati

我正在为我的应用程序启动时设置新的导航。但当我启动时,它会出现在黑色动画中。在黑色动画之后,它会设置导航栏。请告诉我是什么问题

我正在使用下面的代码

var controller = UIViewController()

//App Theming
var navController = UINavigationController()
navController.navigationBar.barTintColor = UIColor.white
navController.navigationBar.tintColor = UIColor.white
navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
navController.navigationBar.shadowImage = UIImage()

navController.navigationBar.setBackgroundImage(UIImage(), for: .default)
navController.navigationBar.isTranslucent = false
navController = UINavigationController(rootViewController: viewcontroller)
navController.navigationBar.isHidden = true

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = navController
appDelegate.window?.makeKeyAndVisible()

问题在于这一行:

navController.navigationBar.isHidden = true

删除它,然后重试

请使用下面的代码片段

在本文中,我使用的是来自主情节提要的ViewController

如果我取消隐藏导航栏

    navigationController.navigationBar.isHidden = false
你可以清楚地看到结果

  • 我可以看出您没有给navigationController任何ViewController。
  • 您需要向导航控制器传递至少一个viewController,以便它知道要启动哪个viewController 导航过程中,请按照以下代码进行操作:

    var storyboard = UIStoryboard(name: "Main", bundle: nil)
    var ivc = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
    navigationController?.pushViewController(anIvc, animated: true)
    window.rootViewController = ivc
    window.rootViewController = navigationController
    window.makeKeyAndVisible()
    
    变量窗口:UIWindow? 设nav=UINavigationController()

1-这里是我声明初始viewController的地方(我希望导航过程从这里开始)

2-这里是我给navigationController第一个开始的viewController的地方

    window?.rootViewController = nav
    nav.viewControllers = [main]//you need to have this line
    nav.isNavigationBarHidden = true
    window?.makeKeyAndVisible()

好吧,我刚注意到你在使用故事板,试试看:

var storyboard = UIStoryboard(name: "Main", bundle: nil)
var ivc = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
navigationController?.pushViewController(anIvc, animated: true)
window.rootViewController = ivc
window.rootViewController = navigationController
window.makeKeyAndVisible()

您的代码应该放在

import UIKit

// AppDelegate class file

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Be attentive. controller allocated withou any layout. Change to custom controller class or load from IB resource (storyboard/nib)
    let controller = UIViewController()
    let navigationController = rootNavigationController
    // Setup viewControllers. Just one controller as root
    navigationController.viewControllers = [controller]
    // You already have a reference to window in your AppDelegate
    window.rootViewController = navigationController
    window.makeKeyAndVisible()
}

extension AppDelegate {

    // Move out of AppDelegate class code to create theming NavigationController
    private var rootNavigationController: UINavigationController {
        let navController = UINavigationController()
        navController.navigationBar.barTintColor = UIColor.white
        navController.navigationBar.tintColor = UIColor.white
        navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        navController.navigationBar.shadowImage = UIImage()
        navController.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navController.navigationBar.isTranslucent = false
        navController.navigationBar.isHidden = true
        // If it theme for all application you should use appearances
        /* For Example
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        UINavigationBar.appearance().tintColor = .white
        */

        return navController
    }

}
您可能会遇到以下错误:

  • 控制器实例没有任何布局。默认情况下,ViewController 什么都没有
  • 你可以在不同的地方打电话。它是市场,由您调用AppDelegate实例。在中配置您的rootViewController 完成装载
  • 您有一个单独的ViewController,作为根用户放置

navController=UINavigationController(rootViewController:viewcontroller)
这行代码很奇怪,为什么需要重新初始化NavigationController?它创建了
UINavigationController
的一个新实例,并将其分配给
navController
变量。这意味着之前的整个
navController
配置代码毫无意义。我这样做是为了给atlas分配一个根视图控制器。那么解决方案是什么呢?您从哪里调用此代码?。什么引用了
navController=UINavigationController(rootViewController:viewcontroller)
中的视图控制器?我有一个splashview控制器,我正在从中创建一个新的导航。但是当我设置根视图控制器时,我需要这个吗?
import UIKit

// AppDelegate class file

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Be attentive. controller allocated withou any layout. Change to custom controller class or load from IB resource (storyboard/nib)
    let controller = UIViewController()
    let navigationController = rootNavigationController
    // Setup viewControllers. Just one controller as root
    navigationController.viewControllers = [controller]
    // You already have a reference to window in your AppDelegate
    window.rootViewController = navigationController
    window.makeKeyAndVisible()
}

extension AppDelegate {

    // Move out of AppDelegate class code to create theming NavigationController
    private var rootNavigationController: UINavigationController {
        let navController = UINavigationController()
        navController.navigationBar.barTintColor = UIColor.white
        navController.navigationBar.tintColor = UIColor.white
        navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        navController.navigationBar.shadowImage = UIImage()
        navController.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navController.navigationBar.isTranslucent = false
        navController.navigationBar.isHidden = true
        // If it theme for all application you should use appearances
        /* For Example
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        UINavigationBar.appearance().tintColor = .white
        */

        return navController
    }

}