Home > Blockchain >  UINavigationBar Large Title doesn't appear when scroll view up
UINavigationBar Large Title doesn't appear when scroll view up

Time:06-13

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?

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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