繁体   English   中英

向上滚动视图时不出现 UINavigationBar 大标题

[英]UINavigationBar Large Title doesn't appear when scroll view up

我已经实现了一项功能,当您按下UITabBar图标并viewController1使用其UIScrollView向上滚动时。 它工作得很好,但是如果我向下滚动view并停止某处,然后切换到另一个viewController2 ,然后返回到viewController1并按tabBar图标 - viewController1将向上滚动,但Large Title永远不会显示,我应该按tabBar图标再展示一次:

我用于向上滚动 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生命的不同阶段跟踪最大的biggestTopSafeAreaInset ,但它始终具有相同的数字 - 196.0。 但是为什么在 viewControllers 切换后它不滚动到Large Title呢?

在您的 tableView 中将 contentInsetAdjustmentBehavior 设置为从不

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
    }

    
}

这是标签栏控制器

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

尝试在 viewDidLoad 中添加:

view.addSubview(UIView())

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

经过一番研究,我发现什么可以解决我的问题。 如果你在tabBarController didSelect中调用这个方法有一个小的延迟,那么在切换 viewController 之后可能会看到一个大标题。 但我仍然无法弄清楚它发生的确切原因......

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM