首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SWIFT5.6 NavigationItem没有显示

SWIFT5.6 NavigationItem没有显示
EN

Stack Overflow用户
提问于 2022-04-19 04:43:34
回答 1查看 51关注 0票数 0

我正在尝试将ImageView添加到NavigationBar中。我在SceneDelegate中做了所有的设置来设置一个rootview控制器,它看起来很有效,但是当我试图添加标题或图像时,它并没有显示出来。

SceneDelegate:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-19 06:04:23

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

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

代码语言:javascript
复制
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()
}

这是MainTabBarController类。

代码语言:javascript
复制
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]
    }
}

现在修改titleView of navigationItem in UIViewController

代码语言:javascript
复制
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
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71919784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档