大家好,我希望你过得愉快。

我一直试图隐藏UINavigationBar附带的UINavigationController一段时间了,最​​终我获得了一些成功,但是问题是每次我从UINavigationController移到另一个或退出应用程序并返回时( UINavigationBar被我的代码隐藏了)UINavigationBar发生了奇怪的事情,我将演示我的代码和发生的GIF。

事情主要发生在


  scrollViewDidScroll


UIScrollViewDelegate方法
然后,当滚动结束时,


  scrollViewDidEndDragging


会跑

正常行为
swift - 无法正确隐藏到UINavigationBar-LMLPHP
返回应用后
swift - 无法正确隐藏到UINavigationBar-LMLPHP

extension GDMainViewController: UIScrollViewDelegate {

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

    if urlField.isFirstResponder {
        urlField.resignFirstResponder()
    }

    let scrollOffset = scrollView.contentOffset.y

    if webScrollLastYAxis == nil {
        webScrollLastYAxis = scrollOffset
    }
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

    let scrollOffset = scrollView.contentOffset.y

    if !decelerate {

        guard let navigationBar = navigationController?.navigationBar else {
            return
        }

        let navOffset = navigationBar.frame.origin.y
        let navViews = navigationBar.subviews[2].subviews


        UIView.animate(withDuration: 0.5) {
            if navOffset < -10 {
                navigationBar.frame.origin.y = self.statusBarFrame
                for view in navViews {
                    view.alpha = 0.0
                }
                self.webScrollIsHidingViews = true
            } else {
                navigationBar.frame.origin.y = 20
                for view in navViews {
                    view.alpha = 1.0
                }
                self.webScrollIsHidingViews = false
            }
        }
    }

    webScrollLastYAxis = scrollOffset
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    guard let navigationBar = navigationController?.navigationBar else {
        return
    }
    guard let oldScrollOffset = webScrollLastYAxis else {
        return
    }
    let scrollOffset = scrollView.contentOffset.y
    let navOffset = navigationBar.frame.origin.y
    let navViews = navigationBar.subviews[2].subviews

    if oldScrollOffset > scrollOffset {
        //print("downwards")
        if navOffset < 21 && navOffset >= statusBarFrame {
            if navOffset != 20 {
                UIView.animate(withDuration: 0.1) {
                    navigationBar.frame.origin.y += 1
                    for view in navViews {
                        let newAlpha = view.alpha + 0.04
                        if newAlpha > 1 { view.alpha = 1.0
                        } else { view.alpha = newAlpha }
                    }
                }
            }
        }
    } else {
        //print("upwards")
        if scrollOffset > 0 {
            if navOffset < 21 && navOffset >= statusBarFrame {
                if navOffset != statusBarFrame {
                    UIView.animate(withDuration: 0.1) {
                        navigationBar.frame.origin.y -= 1
                        for view in navViews {
                            let newAlpha = view.alpha - 0.02
                            if newAlpha < 0 { view.alpha = 0.0
                            } else { view.alpha = newAlpha }
                        }
                    }
                }
            }
        }
    }
}

最佳答案

可能会帮助您-

override func viewWillAppear(_ animated: Bool) {
     self.navigationController?.setNavigationBarHidden(false, animated: true)
     searchBar.isHidden = false

}

关于swift - 无法正确隐藏到UINavigationBar,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55901221/

10-13 09:04