/// <summary> /// Update ScrollView scroll start position based on other scrollview scrolling /// </summary> public static void UpdateScrollViewStart(HeaderLayout headerLayout, ContentPage contentPage, NavigationPage navigationPage, ScrollView scrollView, double scrollY) { bool isNavigationBarFloating = false; NavigationBarVisibility navBarVisibility = NavigationBarVisibility.Hidden; double navigationBarHeight = 0; if (contentPage != null) { isNavigationBarFloating = NavigationBar.GetIsFloating(contentPage); navBarVisibility = NavigationBar.GetVisibility(contentPage); } if (navigationPage != null) { bool isNavigationBarScrollable = navBarVisibility == NavigationBarVisibility.Scroll || navBarVisibility == NavigationBarVisibility.SmoothScroll; if (navigationPage != null && (isNavigationBarFloating || isNavigationBarScrollable)) { navigationBarHeight = navigationPage.GetNavigationBarHeight(); } } if (headerLayout != null) { // If scrolled header hidden if (scrollY >= headerLayout.HeaderHeight + navigationBarHeight) { // If scrollview is scrolled less than header height if (scrollView.ScrollY < headerLayout.HeaderHeight + navigationBarHeight) { if (Device.RuntimePlatform == Device.UWP) { ScrollEffect effect = scrollView.Effects.FirstOrDefault(c => c is ScrollEffect) as ScrollEffect; effect.ScrollTo(0, headerLayout.HeaderHeight + navigationBarHeight); } else { scrollView.ScrollToAsync(0, headerLayout.HeaderHeight + navigationBarHeight, false); } } } // If scrolled less than header height else if (scrollView.ScrollY.Equals(scrollY) == false) { if (Device.RuntimePlatform == Device.UWP) { ScrollEffect effect = scrollView.Effects.FirstOrDefault(c => c is ScrollEffect) as ScrollEffect; effect.ScrollTo(0, scrollY); } else { scrollView.ScrollToAsync(0, scrollY, false); } } } }
/// <summary> /// Update ScrollView / CollectionView padding which are header layout content /// </summary> public static void UpdateScrollSourcePadding(HeaderLayout headerLayout, ContentPage contentPage, NavigationPage navigationPage, View scrollSource) { bool isHeaderLayoutInScrollViewer = headerLayout != null?VisualTreeHelper.GetParent <ScrollView>(headerLayout) != null : false; // Get navigation page navigation bar height double topPadding = 0; if (contentPage != null && NavigationBar.GetIsFloating(contentPage) == false && isHeaderLayoutInScrollViewer == false && (NavigationBar.GetVisibility(contentPage) == NavigationBarVisibility.Scroll || NavigationBar.GetVisibility(contentPage) == NavigationBarVisibility.SmoothScroll)) { topPadding += navigationPage.GetNavigationBarHeight(); } if (headerLayout != null && isHeaderLayoutInScrollViewer == false) { topPadding += headerLayout.HeaderHeight; topPadding += headerLayout.StickyHeaderHeight; } if (scrollSource is ScrollView scrollView) { if (topPadding.Equals(scrollView.Padding.Top) == false) { scrollView.Padding = new Thickness(scrollView.Padding.Left, topPadding, scrollView.Padding.Right, scrollView.Padding.Bottom); } } else if (scrollSource is CollectionView collectionView) { if (collectionView.Header == null) { BoxView b = new BoxView(); b.HeightRequest = 0; collectionView.Header = b; } if (collectionView.Header is View headerView) { headerView.Margin = new Thickness(headerView.Margin.Left, topPadding, headerView.Margin.Right, headerView.Margin.Bottom); } } }
/// <summary> /// Helper to get navigation bar properties /// </summary> public static void TryGetNavigationBarProperties(ContentPage contentPage, NavigationPage navigationPage, out double navigationBarHeight, out double navigationBarY, out bool isNavigationBarFloating, out bool isNavigationBarScrollable) { if (contentPage != null && navigationPage != null) { navigationBarHeight = NavigationBar.GetVisibility(contentPage) != NavigationBarVisibility.Hidden ? navigationPage.GetNavigationBarHeight() : 0; isNavigationBarFloating = NavigationBar.GetIsFloating(contentPage); isNavigationBarScrollable = NavigationBar.GetVisibility(contentPage) == NavigationBarVisibility.Scroll || NavigationBar.GetVisibility(contentPage) == NavigationBarVisibility.SmoothScroll; if (navigationPage.GetNavigationBar() is NavigationBar navigationBar) { navigationBarY = navigationBar.TranslationY; } else { navigationBarY = 0; } } else { navigationBarHeight = 0; navigationBarY = 0; isNavigationBarFloating = false; isNavigationBarScrollable = false; } }