我在发布时为我的应用设置了新导航。但是当我启动它时会出现黑色动画。黑色后会设置导航栏。请告诉我什么是问题。
我正在使用以下代码
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()
答案 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
答案 2 :(得分:0)
您需要至少向导航控制器传递一个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
}
}
您可能的错误: