@@ -32,23 +32,49 @@ public class NavigationBarHider: NSObject {
3232
3333 fileprivate var isUserScrolling : Bool = false
3434 fileprivate var isScrollingToTop : Bool = false
35+ fileprivate var isProgramaticallyScrolling : Bool = false
3536 var initialScrollY : CGFloat = 0
3637 var initialNavigationBarPercentHidden : CGFloat = 0
38+
39+ private func setDataForScroll( yOffset: CGFloat , topContentInset: CGFloat ) {
40+ guard let navigationBar = navigationBar else {
41+ return
42+ }
43+ initialScrollY = yOffset + topContentInset
44+ initialNavigationBarPercentHidden = navigationBar. navigationBarPercentHidden
45+ }
46+
47+ public func setIsProgramaticallyScrolling( _ isProgramaticallyScrolling: Bool , yOffset: CGFloat , topContentInset: CGFloat ) {
48+ guard navigationBar? . isInteractiveHidingEnabled == true else {
49+ return
50+ }
51+
52+ self . isProgramaticallyScrolling = isProgramaticallyScrolling
53+
54+ if isProgramaticallyScrolling {
55+ setDataForScroll ( yOffset: yOffset, topContentInset: topContentInset)
56+ }
57+ }
3758
3859 @objc public func scrollViewWillBeginDragging( _ scrollView: UIScrollView ) {
39- guard let navigationBar = navigationBar , navigationBar . isInteractiveHidingEnabled else {
60+ guard navigationBar? . isInteractiveHidingEnabled == true else {
4061 return
4162 }
4263 isUserScrolling = true
43- initialScrollY = scrollView. contentOffset. y + scrollView. contentInset. top
44- initialNavigationBarPercentHidden = navigationBar. navigationBarPercentHidden
64+ setDataForScroll ( yOffset: scrollView. contentOffset. y, topContentInset: scrollView. contentInset. top)
4565 }
4666
4767 @objc public func scrollViewDidScroll( _ scrollView: UIScrollView ) {
4868 guard let navigationBar = navigationBar else {
4969 return
5070 }
51-
71+
72+ if isProgramaticallyScrolling {
73+ /// When programatically scrolling on a just-created scroll view, we need a layout pass to get accurate heights.
74+ scrollView. layoutIfNeeded ( )
75+ navigationBar. layoutIfNeeded ( )
76+ }
77+
5278 guard scrollView. contentSize. height > 0 else {
5379 if navigationBar. isAdjustingHidingFromContentInsetChangesEnabled {
5480 navigationBar. setNavigationBarPercentHidden ( 0 , underBarViewPercentHidden: 0 , extendedViewPercentHidden: 0 , topSpacingPercentHidden: 0 , animated: false )
@@ -78,7 +104,7 @@ public class NavigationBarHider: NSObject {
78104 }
79105 }
80106
81- guard navigationBar. isInteractiveHidingEnabled, isUserScrolling || isScrollingToTop || scrollY < totalHideableHeight else {
107+ guard navigationBar. isInteractiveHidingEnabled, isUserScrolling || isScrollingToTop || isProgramaticallyScrolling || scrollY < totalHideableHeight else {
82108 return
83109 }
84110
@@ -234,8 +260,7 @@ public class NavigationBarHider: NSObject {
234260 guard let navigationBar = navigationBar, navigationBar. isInteractiveHidingEnabled else {
235261 return
236262 }
237- initialNavigationBarPercentHidden = navigationBar. navigationBarPercentHidden
238- initialScrollY = scrollView. contentOffset. y + scrollView. contentInset. top
263+ setDataForScroll ( yOffset: scrollView. contentOffset. y, topContentInset: scrollView. contentInset. top)
239264 isScrollingToTop = true
240265 }
241266
0 commit comments