Swift 5.6 NavigationItem不显示

发布于 2025-01-22 17:59:32 字数 1672 浏览 0 评论 0原文

我正在尝试将ImageView添加到NavigationBar。我在SceneDelegate中进行了所有设置,以设置一个rootviewController,并且似乎有效,但是当我尝试添加标题或图像时,它确实没有显示。

SceneDelegate:

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 NavigationController:

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
        }
    }
}

I am trying to add ImageView to NavigationBar. I did all setups in SceneDelegate to setup a rootviewcontroller and seems like it works, but when I try to add Title or Image it does no show it.

SceneDelegate :

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
NavigationController:

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
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2025-01-29 17:59:32

请勿在uinavigationController中嵌入uitabbarController。宁愿将每个uiviewController嵌入uinavigationController之前,然后将它们添加到uitabbarcontroller。这是一个例子。

修改场景(_: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()
}

这是MainterabbarController类。

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]
    }
}

现在,navigationItemuiviewController中修改titleView navigationItem 。

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
        }
    }
}

Do not embed UITabBarController in UINavigationController. Rather embed each UIViewController into UINavigationController before adding them to UITabBarController. Here is an example.

Modify the scene(_:willConnectTo:options) method like below.

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

Here is the MainTabBarController class.

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]
    }
}

Now modify titleView of navigationItem in UIViewController.

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
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文