Swift 5.6 NavigationItem不显示

Swift 5.6 NavigationItem does not show

我正在尝试将 ImageView 添加到 NavigationBar。我在 SceneDelegate 中完成了所有设置以设置 rootviewcontroller 并且看起来它可以工作,但是当我尝试添加标题或图像时它没有显示它。

场景代理:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        guard let scene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: scene)
        window?.rootViewController = UINavigationController(rootViewController: MainTabController())
        window?.makeKeyAndVisible()
    }

NavigationBar 导航控制器:

class FeedController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        configureUI()
        setupNavController()
    }
    
    func configureUI() {
        view.backgroundColor = .white

        let imageView = UIImageView(image: UIImage(named: "twitter_logo_blue"))
        imageView.contentMode = .scaleAspectFit
        navigationItem.titleView = imageView
   
    }
    
    @objc func addTapped() {
        //
    }
    
    func setupNavController() {
        if #available(iOS 15.0, *) {
            let navigationBarAppearance = UINavigationBarAppearance()
            navigationBarAppearance.configureWithDefaultBackground()
            UINavigationBar.appearance().standardAppearance = navigationBarAppearance
            UINavigationBar.appearance().compactAppearance = navigationBarAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
        }
    }
}

不要在 UINavigationController 中嵌入 UITabBarController。而是先将每个 UIViewController 嵌入 UINavigationController,然后再将它们添加到 UITabBarController。这是一个例子。

修改 scene(_:willConnectTo:options) 方法如下。

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    let window = UIWindow(windowScene: windowScene)
    window.rootViewController = MainTabViewController()
    self.window = window
    window.makeKeyAndVisible()
}

这里是MainTabBarControllerclass.

class MainTabViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainVC = UINavigationController(rootViewController: ViewController())
        mainVC.tabBarItem = UITabBarItem(title: "Tab 1", image: UIImage(systemName: "circle"), selectedImage: UIImage(systemName: "circle.fill"))
        let secondVC = UINavigationController(rootViewController: SecondViewController())
        secondVC.tabBarItem = UITabBarItem(title: "Tab 2", image: UIImage(systemName: "square"), selectedImage: UIImage(systemName: "square.fill"))
        
        viewControllers = [mainVC, secondVC]
    }
}

现在修改 UIViewControllernavigationItemtitleView

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureUI()
        setupNavController()
    }
    
    func configureUI() {
        view.backgroundColor = .white
        
        let imageView = UIImageView(image: UIImage(systemName: "sun.max.circle.fill"))
        imageView.contentMode = .scaleAspectFit
        navigationItem.titleView = imageView
        
    }
    
    func setupNavController() {
        if #available(iOS 15.0, *) {
            
            let navigationBarAppearance = UINavigationBarAppearance()
            navigationBarAppearance.configureWithDefaultBackground()
            UINavigationBar.appearance().standardAppearance = navigationBarAppearance
            UINavigationBar.appearance().compactAppearance = navigationBarAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
        }
    }
}