As stated at the title
Is it possible to hide the scrollbar in ScrollView Xamarin.Forms without having to create a custom renderer for each platform?
I can't seem to find any answers, suggestions, or examples that are not using custom renderer.
edit Mar 7, 2018: for Googleability: a PR was merged which adds this ability to the default Xamarin.Forms toolkit. It should be released in the NuGets soon.
Original answer:
As stated in the comments you are not able to achieve this without the use of a CustomRenderer.
Create a class ScrollViewExRenderer in your iOS and Droid project and put this in it, the code taken from the link in the comments as well;
public class ScrollViewExRenderer : ScrollViewRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement == null || this.Element == null)
return;
if (e.OldElement != null)
e.OldElement.PropertyChanged -= OnElementPropertyChanged;
e.NewElement.PropertyChanged += OnElementPropertyChanged;
}
protected void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (ChildCount > 0)
{
GetChildAt(0).HorizontalScrollBarEnabled = false;
GetChildAt(0).VerticalScrollBarEnabled = false;
}
}
}
Above the namespace declaration put this line;
[assembly: ExportRenderer(typeof(ScrollView), typeof(ScrollViewExRenderer))]
Now your ScrollViews should not show any scrollbars
OnElementChanged. The method will never execute the line e.OldElement.PropertyChanged -= OnElementPropertyChanged; because it would require e.OldElement != null, but if that's true, the method will exit out from the if statement above it. Unless I'm missing something?Hey this seems a bit late to post an answer but since you haven't selected an answer i will post the procedure for android and windows projects. The answer given by gerald might have been true at some point but i couldn't get it to work maybe because xamarin has changed a lot.
Create a new class in .DROID and .UWP projects and add the following code
For .UWP
using Windows.UI.Xaml.Controls;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;
[assembly: ExportRenderer(typeof(ScrollView), typeof(App3.UWP.Scrollbardisabledrenderer))]
namespace App3.UWP
{
public class Scrollbardisabledrenderer : ScrollViewRenderer
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
Control.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
Control.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
}
}
}
For .DROID
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(ScrollView), typeof(App3.Droid.Scrollbardisabledrenderer))]
namespace App3.Droid
{
public class Scrollbardisabledrenderer : ScrollViewRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null || this.Element == null)
return;
if (e.OldElement != null)
e.OldElement.PropertyChanged -= OnElementPropertyChanged;
e.NewElement.PropertyChanged += OnElementPropertyChanged;
}
protected void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
this.HorizontalScrollBarEnabled = false;
this.VerticalScrollBarEnabled = false;
}
}
}
I'm sorry about .IOS I can't confirm whether my code is working or not because I dont have a mac.
If you have vertical ScrollView, you can put VerticalScrollBarVisibility="Never" in your xaml:
<ScrollView Orientation="Vertical" VerticalScrollBarVisibility="Never">
If you have horizontal ScrollView, you can put HorizontalScrollBarVisibility ="Never" in your xaml:
<ScrollView Orientation="Horizontal" HorizontalScrollBarVisibility ="Never">
To hide ScrollBar in XamarinForms on my Android solution I used this:
https://forums.xamarin.com/discussion/73353/how-to-hide-scroll-bar-in-scrollview
But for IOS you have to change a code a bit:
public class ScrollBarDisabledRenderer : ScrollViewRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
return;
if (e.OldElement != null)
e.OldElement.PropertyChanged -= OnElementPropertyChanged;
e.NewElement.PropertyChanged += OnElementPropertyChanged;
}
private void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
ShowsHorizontalScrollIndicator = false;
ShowsVerticalScrollIndicator = false;
}
}
Above the namespace declaration:
[assembly: ExportRenderer(typeof(MyScrollView), typeof(ScrollBarDisabledRenderer))]
So, name of properties is different for IOS:
ShowsHorizontalScrollIndicator = false;
ShowsVerticalScrollIndicator = false;
this code works on Android and for IOS replace :
this.HorizontalScrollBarEnabled = false;
this.VerticalScrollBarEnabled = false;
by
ShowsHorizontalScrollIndicator = false;
ShowsVerticalScrollIndicator = false;
CustomRendererCustomRenderer@GeraldVersluis I see, could you please give me a pointer to understand how to useCustomRenderer?