您的控制器绝对不是导航控制器,如果它是您在应用程序中出现的第一个控制器,您必须在 willConnectTo 函数下的场景委托中设置它,如下所示:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let controller = UINavigationController(rootViewController: ViewController())
window?.rootViewController = controller
}
如果您使用按钮操作集函数调用控制器,该函数呈现如下导航控制器:
@objc fileprivate func callNavigationController() {
let controller = UINavigationController(rootViewController: YourController())
controller.modalPresentationStyle = .fullScreen
present(controller, animated: true, completion: nil)
}
现在在 viewDidLoad 中设置 navigationBar 背景,
首先使用 guard let 语句打开图像:
guard let logo = UIImage(named: "navigationbar") else { return }
设置导航栏背景图片后:
navigationController?.navigationBar.setBackgroundImage(logo.resizableImage(withCapInsets: UIEdgeInsets(top: 0,left: 0, bottom: 0 ,right: 0), resizingMode: .stretch), for: .default)
仅此而已