导航栏显示为黑色?

时间:2018-06-14 05:40:38

标签: ios swift uinavigationbar

我在发布时为我的应用设置了新导航。但是当我启动它时会出现黑色动画。黑色后会设置导航栏。请告诉我什么是问题。

我正在使用以下代码

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()

5 个答案:

答案 0 :(得分:3)

问题是这一行:

navController.navigationBar.isHidden = true

删除它,然后重试。

答案 1 :(得分:3)

请使用以下代码段

  

在此,我正在使用Main Storyboard中的ViewController

   // mainStoryboard
        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)

        // rootViewController
        let rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController

        // navigationController
        let navigationController = UINavigationController(rootViewController: rootViewController!)

        //App Theming
        navigationController.navigationBar.barTintColor = UIColor.white
        navigationController.navigationBar.tintColor = UIColor.white
        navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        navigationController.title = "Testing Th"
        navigationController.navigationBar.shadowImage = UIImage()


        navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController.navigationBar.isTranslucent = false
        navigationController.navigationBar.isHidden = true
        // self.window
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window!.rootViewController = navigationController
        self.window!.makeKeyAndVisible()

如果我取消隐藏导航栏

    navigationController.navigationBar.isHidden = false

您可以清楚地看到结果 enter image description here

答案 2 :(得分:0)

  • 我可以看到您没有给navigationController提供任何viewControllers。
  • 您需要至少向导航控制器传递一个viewController,以使其知道要启动哪个viewController 导航过程,请遵循以下代码:

    var window:UIWindow? let nav = UINavigationController()

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

var  main = HomeViewController(nibName: "HomeViewController", bundle: nil)

2-这是我给navigationController第一个要从其开始的viewController的地方。

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

答案 3 :(得分:0)

好吧,我刚刚注意到您正在使用情节提要,请尝试一下:

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()

答案 4 :(得分:0)

您的代码应放在

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 didFinishLoading
  • 您有一个单独的ViewController放置为根