How to disable Carousel Page scrolling in iOS

Hello there.

I thought I'd share this bit of code with you guys since I wasn't able to find it anywhere on the web.

using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(CarouselPage), typeof(CustomCarouselPageRenderer))]
namespace App.iOS
{
    public class CustomCarouselPageRenderer : CarouselPageRenderer
    {
        // Override the OnElementChanged method so we can tweak this renderer post-initial setup
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            UIView view = this.NativeView;
            UIScrollView scrollView = (UIKit.UIScrollView)view.Subviews[0];
            scrollView.ScrollEnabled = false;
        }
    }
}

Posts

  • CaioshinnCaioshinn USMember ✭✭
    edited April 2018

    thank you for your suggestion, it was a good point to start! In my solution it prevents undesired vertical scroll but at the same time it doesn't leave the pages free to swipe from left to right.

    The solution that solves the vertical scroll problem for me was achieved with this code:

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
    base.OnElementChanged(e);

            UIView view = this.NativeView;
            UIScrollView scrollView = (UIKit.UIScrollView)view.Subviews[0];
            scrollView.ShowsVerticalScrollIndicator = false;
            scrollView.ContentSize = new CoreGraphics.CGSize(scrollView.ContentSize.Width, scrollView.Frame.Size.Height);
            AutomaticallyAdjustsScrollViewInsets = false;
        }
    
  • jtthjtth Member ✭✭

    @AhmedElashker said:
    Hello there.

    I thought I'd share this bit of code with you guys since I wasn't able to find it anywhere on the web.

    using UIKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    
    [assembly: ExportRenderer(typeof(CarouselPage), typeof(CustomCarouselPageRenderer))]
    namespace App.iOS
    {
        public class CustomCarouselPageRenderer : CarouselPageRenderer
        {
            // Override the OnElementChanged method so we can tweak this renderer post-initial setup
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                UIView view = this.NativeView;
                UIScrollView scrollView = (UIKit.UIScrollView)view.Subviews[0];
                scrollView.ScrollEnabled = false;
            }
        }
    }
    

    How to implement in Android? this.RootView?

  • jtthjtth Member ✭✭

    @AhmedElashker said:
    Hello there.

    I thought I'd share this bit of code with you guys since I wasn't able to find it anywhere on the web.

      UIView view = this.NativeView;
      UIScrollView scrollView = (UIKit.UIScrollView)view.Subviews[0];
      scrollView.ScrollEnabled = false;
    

    How to implement in Android?

    var view = this.RootView;
    ?
    ?

    [assembly: ExportRenderer(typeof(CarouselPage), typeof(CustomCarouselPageRenderer))]
    namespace XamFormsApp.Droid.Renderers
    {
        public class CustomCarouselPageRenderer : VisualElementRenderer<CarouselPage>
        {
            public CustomCarouselPageRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<CarouselPage> e)
            {
                base.OnElementChanged(e);
            }
        }
    }
    
  • jtthjtth Member ✭✭

    Figured it out, SO

Sign In or Register to comment.