简体   繁体   中英

large titles with search bar animation glitch

When going back from view controller with:

navigationItem.largeTitleDisplayMode = .never

to view controller with:

navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always

in viewDidLoad I have a strange animation glitch on navigation bar - a white stripe appears

It's appears only when searchBar is visible

动画录制

How can I fix this?

I found problem in appear transition, there are some clear space between navigationbar and searchbar. They have independent animation. Here is it. So, you can add common background for them throw transition in "viewWillAppear" method using transitionCoordinator. That its view.

edited : strong text add logic, when your transition from search table, and navigation is hide at this moment. (searchController.hidesNavigationBarDuringPresentation = true)

override func viewWillAppear(_ animated: Bool) {
    let navigationBack = UIView()
    if navigationItem.searchController?.isActive == false {
        navigationBack.frame = self.navigationController?.navigationBar.frame ?? CGRect.zero
    }
    navigationBack.backgroundColor = navigationController?.navigationBar.barTintColor
    let containerView = transitionCoordinator?.containerView

    transitionCoordinator?.animateAlongsideTransition(in: containerView, animation: { (context) in
        containerView?.addSubview(navigationBack)
        navigationBack.frame.size.height += self.navigationItem.searchController?.searchBar.frame.height ?? 0
    }, completion: { (context) in
        navigationBack.removeFromSuperview()
    })

    super.viewWillAppear(animated)
}

将导航控制器视图的背景色设置为与导航栏相同的颜色对我来说似乎可以解决此问题。

navigationController?.view.backgroundColor = navigationController?.navigationBar.barTintColor

What helped was to add this in viewController with largeTitles enabled:

override func viewWillAppear(_ animated: Bool) {
    let navigationBack = UIView()
    navigationBack.frame = (self.navigationController?.navigationBar.frame)!
    navigationBack.frame.size.height = 44
    navigationBack.backgroundColor = navigationController?.navigationBar.barTintColor
    let containerView = transitionCoordinator?.containerView

    transitionCoordinator?.animateAlongsideTransition(in: containerView, animation: { (context) in
        containerView?.addSubview(navigationBack)
        navigationBack.frame.size.height = (self.navigationItem.searchController?.searchBar.frame.height)! + (self.navigationController?.navigationBar.frame.height)!
    }, completion: { (context) in
        navigationBack.removeFromSuperview()
    })

    super.viewWillAppear(animated)
}

and this in viewController with largeTitles disabled:

override func viewWillAppear(_ animated: Bool) {
    let navigationBack = UIView()
    navigationBack.frame = self.navigationController?.navigationBar.frame ?? CGRect.zero
    navigationBack.backgroundColor = navigationController?.navigationBar.barTintColor
    let containerView = transitionCoordinator?.containerView

    transitionCoordinator?.animateAlongsideTransition(in: containerView, animation: { (context) in
        containerView?.addSubview(navigationBack)
        navigationBack.frame.size.height = 44
    }, completion: { (context) in
        navigationBack.removeFromSuperview()
    })

    super.viewWillAppear(animated)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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