uinavigationbar大标题当滚动查看时不会出现

发布于 2025-02-07 07:44:25 字数 1676 浏览 2 评论 0 原文

我已经实现了一个功能,当您按 uitabbar 图标和 view controller1 使用其 uiscrollview 滚动时。它可以很好地工作,但是如果我滚动查看向下并停止某个地方,然后切换到另一个 view controller2 ,然后返回到 view> view> view controller1 ,然后按 tabbar 图标 - viewController1 将滚动,但是大标题将永远不会显示,我应该按 tabbar iCON一个更多时间显示它:

我用于滚动VC1的代码:

private var biggestTopSafeAreaInset: CGFloat = 0

    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()
        self.biggestTopSafeAreaInset = max(view.safeAreaInsets.top, biggestTopSafeAreaInset)
    }
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if tabBarController.selectedIndex == 0 {
            let navigationVC = viewController as? UINavigationController
            let firstVC = navigationVC?.viewControllers.first as? CurrencyViewController
            guard let scrollView = firstVC?.view.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView else { return }
            
            if traitCollection.verticalSizeClass == .compact {
                scrollView.setContentOffset(CGPoint(x: 0, y: -view.safeAreaInsets.top, animated: true)
            } else {
                scrollView.setContentOffset(CGPoint(x: 0, y: -biggestTopSafeAreaInset, animated: true)
            }
        }
    }

我尝试在 vc1 life的不同阶段跟踪 bigaltopsafeareainset ,但它始终具有相同的数字 - 196.0。但是,为什么它不会滚动,直到大标题在ViewControllers Switch之后?

I have implemented a feature, when you press on a UITabBar icon and viewController1 scrolls up using its UIScrollView. It works perfectly, but if I scroll view down and stop somewhere, then switch to another viewController2, then get back to viewController1 and press tabBar icon - the viewController1 will scroll up, but Large Title will never be showed, and I should press tabBar icon one more time to show it:

The code I use for scroll up the VC1:

private var biggestTopSafeAreaInset: CGFloat = 0

    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()
        self.biggestTopSafeAreaInset = max(view.safeAreaInsets.top, biggestTopSafeAreaInset)
    }
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if tabBarController.selectedIndex == 0 {
            let navigationVC = viewController as? UINavigationController
            let firstVC = navigationVC?.viewControllers.first as? CurrencyViewController
            guard let scrollView = firstVC?.view.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView else { return }
            
            if traitCollection.verticalSizeClass == .compact {
                scrollView.setContentOffset(CGPoint(x: 0, y: -view.safeAreaInsets.top, animated: true)
            } else {
                scrollView.setContentOffset(CGPoint(x: 0, y: -biggestTopSafeAreaInset, animated: true)
            }
        }
    }

I tried to track biggestTopSafeAreaInset in different stages of VC1 life, but it always has the same number - 196.0. But then why it doesn't scroll till the Large Title after viewControllers switch?

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

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

发布评论

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

评论(3

尐籹人 2025-02-14 07:44:25

在您的表观视图中,将ContentInsetAdjustmentBehavior在Controller中不在

tableView.contentInsetAdjustmentBehavior = .never

控制器中,再次更新导航栏UI

  override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        DispatchQueue.main.async { [weak self] in
            self?.navigationController?.navigationBar.sizeToFit()
        }
       
    }

,这是导航控制器

class BaseNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if #available(iOS 15.0, *) {
            let scrollAppearance = UINavigationBarAppearance()
            scrollAppearance.shadowColor = .white
            scrollAppearance.backgroundColor = .white
            let navigationBarAppearance = UINavigationBarAppearance()
            navigationBarAppearance.configureWithDefaultBackground()
            navigationBarAppearance.backgroundColor = .white
            navigationBarAppearance.largeTitleTextAttributes = [
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
                NSAttributedString.Key.foregroundColor: UIColor.black
            ]
            navigationBarAppearance.titleTextAttributes = [
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
                NSAttributedString.Key.foregroundColor: UIColor.black
            ]
            UINavigationBar.appearance().backIndicatorImage = UIImage(named: "back-arrow")
            UINavigationBar.appearance().standardAppearance = navigationBarAppearance
            UINavigationBar.appearance().compactAppearance = navigationBarAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = scrollAppearance
            navigationBar.tintColor = .black
            navigationBar.prefersLargeTitles = true
            navigationBar.isTranslucent = false
            navigationItem.largeTitleDisplayMode = .automatic
        } else {
            navigationBar.largeTitleTextAttributes = [
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
                NSAttributedString.Key.foregroundColor: UIColor.black
            ]
            navigationBar.titleTextAttributes = [
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
                NSAttributedString.Key.foregroundColor: UIColor.black
            ]
            navigationBar.tintColor = .black
            navigationBar.prefersLargeTitles = true
            navigationBar.isTranslucent = false
            navigationItem.largeTitleDisplayMode = .automatic
            navigationBar.barTintColor = .white
        }
        
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .darkContent
    }

    
}

,这是Tabbar Controller

class TabbarController:UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let c1 = C1()
        let c2 = C2()
        let c3 = C3()
        
        c1.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named:  "home786"), tag: 0)
        c1.tabBarItem.tag = 0
        let nav1 = BaseNavigationController(rootViewController: c1)
        
        c2.tabBarItem = UITabBarItem(title: "Setting", image: UIImage(named:  "home786"), tag: 0)
        c2.tabBarItem.tag = 1
        let nav2 = BaseNavigationController(rootViewController: c2)
        c2.tabBarItem = UITabBarItem(title: "User", image: UIImage(named:  "home786"), tag: 0)
        c2.tabBarItem.tag = 2
        let nav3 = BaseNavigationController(rootViewController: c3)
        viewControllers = [nav1,nav2,nav3]
        selectedViewController = nav1
        tabBarController?.viewControllers?.first?.view.backgroundColor = .red
        
    }
}

in your tableView set contentInsetAdjustmentBehavior to never

tableView.contentInsetAdjustmentBehavior = .never

in controller update the ui of navigation bar again

  override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        DispatchQueue.main.async { [weak self] in
            self?.navigationController?.navigationBar.sizeToFit()
        }
       
    }

here is the navigation controller

class BaseNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if #available(iOS 15.0, *) {
            let scrollAppearance = UINavigationBarAppearance()
            scrollAppearance.shadowColor = .white
            scrollAppearance.backgroundColor = .white
            let navigationBarAppearance = UINavigationBarAppearance()
            navigationBarAppearance.configureWithDefaultBackground()
            navigationBarAppearance.backgroundColor = .white
            navigationBarAppearance.largeTitleTextAttributes = [
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
                NSAttributedString.Key.foregroundColor: UIColor.black
            ]
            navigationBarAppearance.titleTextAttributes = [
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
                NSAttributedString.Key.foregroundColor: UIColor.black
            ]
            UINavigationBar.appearance().backIndicatorImage = UIImage(named: "back-arrow")
            UINavigationBar.appearance().standardAppearance = navigationBarAppearance
            UINavigationBar.appearance().compactAppearance = navigationBarAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = scrollAppearance
            navigationBar.tintColor = .black
            navigationBar.prefersLargeTitles = true
            navigationBar.isTranslucent = false
            navigationItem.largeTitleDisplayMode = .automatic
        } else {
            navigationBar.largeTitleTextAttributes = [
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
                NSAttributedString.Key.foregroundColor: UIColor.black
            ]
            navigationBar.titleTextAttributes = [
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
                NSAttributedString.Key.foregroundColor: UIColor.black
            ]
            navigationBar.tintColor = .black
            navigationBar.prefersLargeTitles = true
            navigationBar.isTranslucent = false
            navigationItem.largeTitleDisplayMode = .automatic
            navigationBar.barTintColor = .white
        }
        
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .darkContent
    }

    
}

here is the Tabbar Controller

class TabbarController:UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let c1 = C1()
        let c2 = C2()
        let c3 = C3()
        
        c1.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named:  "home786"), tag: 0)
        c1.tabBarItem.tag = 0
        let nav1 = BaseNavigationController(rootViewController: c1)
        
        c2.tabBarItem = UITabBarItem(title: "Setting", image: UIImage(named:  "home786"), tag: 0)
        c2.tabBarItem.tag = 1
        let nav2 = BaseNavigationController(rootViewController: c2)
        c2.tabBarItem = UITabBarItem(title: "User", image: UIImage(named:  "home786"), tag: 0)
        c2.tabBarItem.tag = 2
        let nav3 = BaseNavigationController(rootViewController: c3)
        viewControllers = [nav1,nav2,nav3]
        selectedViewController = nav1
        tabBarController?.viewControllers?.first?.view.backgroundColor = .red
        
    }
}
失眠症患者 2025-02-14 07:44:25

尝试在ViewDidload中添加此内容:

view.addSubview(UIView())

此单行块大标题导航栏...我不知道为什么,但是此技巧会暂时解决问题...

Try to add this in viewDidLoad:

view.addSubview(UIView())

this single line block large title navigation Bar... I don't Know why, but this trick fix momentarily the issue...

倾城花音 2025-02-14 07:44:25

经过一番研究,我发现什么可以解决我的问题。如果您以 TABBARCONTROLLER didSelect 的少量延迟调用此方法,则可以在切换ViewControllers后看到一个大标题。但是我仍然无法确切地弄清楚为什么会发生...

 DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
       navigationVC?.navigationBar.sizeToFit()
 }

After some research I found out what can fix my problem. If you call this method with a small delay in tabBarController didSelect then it will be possible to see a Large Title after switching viewControllers. But I still can't figure out exactly why it happened...

 DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
       navigationVC?.navigationBar.sizeToFit()
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文