无法正确隐藏到UINavigationBar

时间:2019-04-29 10:13:03

标签: swift uiscrollview uinavigationbar

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

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

主要发生在

  

scrollViewDidScroll

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

中的代码
  

scrollViewDidEndDragging

将运行

正常行为 behavior 返回应用后 after switching views or exiting app

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

1 个答案:

答案 0 :(得分:0)

可能对此有帮助-

<script src="https://code.jquery.com/jquery-3.4.0.js" integrity="sha256-DYZMCC8HTC+QDr5QNaIcfR7VSPtcISykd+6eSmBW5qo=" crossorigin="anonymous">
</script>

<button id="flipbutton">flip the card</button>
<div class="flipcardHold">
  <div class="card ss">
    <div class="flipcardFront">
      <!--I want the below divs to be next to each other -->
      <div id="Frontinnerdiv1" class="putmenextTo">Front First div</div>
      <div id="Frontinnerdiv2" class="putmenextTo">Front Second div</div>
      <div id="Frontinnerdiv3" class="putmenextTo">Front Third div</div>
    </div>
    <div class="flipcardBack">
      <!--I want the below divs to be next to each other -->
      <div id="Backinnerdiv1" class="putmenextTo">Back First div</div>
      <div id="Backinnerdiv2" class="putmenextTo">Back Second div</div>
      <div id="Backinnerdiv3" class="putmenextTo">Back Third div</div>
    </div>
  </div>
</div>



<script>
  $("#flipbutton").click(function() {
    $('.card').toggleClass('flipped');
  });
</script>