Google Hide ScrollView scrollbar in Xamarin.Forms (C# - Xaml) | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Sunday, 31 March 2019

Hide ScrollView scrollbar in Xamarin.Forms (C# - Xaml)

Introduction:
A Xamarin.Forms ScrollView contains layouts and enables them to scroll offscreen. There are some situations when we want to hide default vertical/horizontal scrollbar for ScrollView in xamarin.forms. So we can learn it from this article.
Requirements:
  • This article source code was prepared by using Visual Studio Professional for Mac (7.6.9). And it is better to install latest visual studio updates from here.
  • This sample project is Xamarin.Forms .NetStandardLibrary project and tested in Android emulator and iOS simulators.
  • Used Xamarin.Forms version is 3.0.0.561731.
Description:
Update: From Xamarin.Forms version 3.1.0, we can use VerticalScrollBarVisibility, HorizontalScrollBarVisibility properties to handle ScrollView ScrollBarVisibility.

However for older versions, we can hide default ScrollView scrollbar using custom renderers.
1. Xamarin.Android Project:
In Android project, create a class name is ScrollbarDisabledRenderer and make sure to add renderer registration for our ScrollView class on above of the namespace.
  1. using Xamarin.Forms;  
  2. using Xamarin.Forms.Platform.Android;  
  3. using HideScrollViewScroll.Droid.CustomRenderers;  
  4. using System.ComponentModel;  
  5.   
  6. [assembly: ExportRenderer(typeof(ScrollView), typeof(ScrollbarDisabledRenderer))]  
  7. namespace HideScrollViewScroll.Droid.CustomRenderers  
  8. {  
  9.     public class ScrollbarDisabledRenderer : ScrollViewRenderer  
  10.     {  
  11.         protected override void OnElementChanged(VisualElementChangedEventArgs e)  
  12.         {  
  13.             base.OnElementChanged(e);  
  14.   
  15.             if (e.OldElement != null || this.Element == null)  
  16.                 return;  
  17.   
  18.             if (e.OldElement != null)  
  19.                 e.OldElement.PropertyChanged -= OnElementPropertyChanged;  
  20.   
  21.             e.NewElement.PropertyChanged += OnElementPropertyChanged;  
  22.         }  
  23.   
  24.         protected void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)  
  25.         {  
  26.             this.HorizontalScrollBarEnabled = false;  
  27.             this.VerticalScrollBarEnabled = false;  
  28.         }  
  29.     }  
  30. }  
2. Xamarin.iOS Project:
In iOS project, create a class name is ScrollbarDisabledRenderer and make sure to add renderer registration for our ScrollView class on above of the namespace.
  1. using System;  
  2. using Xamarin.Forms;  
  3. using Xamarin.Forms.Platform.iOS;  
  4. using System.ComponentModel;  
  5. using HideScrollViewScroll.iOS.CustomRenderers;  
  6.   
  7. [assembly: ExportRenderer(typeof(ScrollView), typeof(ScrollbarDisabledRenderer))]  
  8. namespace HideScrollViewScroll.iOS.CustomRenderers  
  9. {  
  10.     public class ScrollbarDisabledRenderer : ScrollViewRenderer  
  11.     {  
  12.         protected override void OnElementChanged(VisualElementChangedEventArgs e)  
  13.         {  
  14.             base.OnElementChanged(e);  
  15.             if (e.OldElement != null || Element == null)  
  16.                 return;  
  17.   
  18.             if (e.OldElement != null)  
  19.                 e.OldElement.PropertyChanged -= OnElementPropertyChanged;  
  20.             e.NewElement.PropertyChanged += OnElementPropertyChanged;  
  21.         }  
  22.   
  23.         private void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)  
  24.         {  
  25.             ShowsHorizontalScrollIndicator = false;  
  26.             ShowsVerticalScrollIndicator = false;  
  27.         }  
  28.     }  
  29. }  
You can also directly work on below sample source code to understand this article.
HideScrollBar

FeedBack Note: Please share your thoughts, what you think about this post, Is this post really helpful for you? I always welcome if you drop comments on this post and it would be impressive.

Follow me always at @Subramanyam_B
Have a nice day by  :)

No comments:

Post a Comment

Search Engine Submission - AddMe