Why ModalTransitionStyle is ignored in Xamarin.Forms? Are any workarounds possible?

rudyrykrudyryk RUMember ✭✭✭
edited August 2016 in Xamarin.Forms

I'm trying to customize modals animation transition via custom renderer, but with no luck. Here's my code:

using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;
using Project.Portable;
using Project.iOS;

[assembly: ExportRenderer (typeof (MainPage), typeof (MainPageRenderer))]

namespace Project.iOS
{
    public class MainPageRenderer : PageRenderer
    {
        protected override void OnElementChanged (VisualElementChangedEventArgs e)
        {
            base.OnElementChanged (e);

            ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
            ViewController.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
        }
    }
}

and in App.cs:

protected void NavigateToStart ()
{
    Device.BeginInvokeOnMainThread (async () => {
        var main = new MainPage ();
        await MainPage.Navigation.PushModalAsync (main, true);
    });
}

It seems like ModalTransitionStyle is just ignored. If then, why it's exposed in the interface after all? It's very confusing and time wasting.

Posts

  • rudyrykrudyryk RUMember ✭✭✭
    edited August 2016

    Yeah! I did it :) There IS a workaround!!!

    The reason is that the view controller for modal page is wrapped into internal ModalWrapper class: PlatformRenderer.cs#L11

    And the wrapper instance does not set underlying view controller properties. But we can tweak the wrapper from DidMoveToParentViewController method just like that:

    namespace Project.iOS
    {
        public class MainPageRenderer : PageRenderer
        {
            public override void DidMoveToParentViewController (UIViewController parent)
            {
                base.DidMoveToParentViewController (parent);
                parent.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
            }
        }
    }
    

    Voila! ;)

  • RonTolliverRonTolliver USUniversity ✭✭

    I was just struggling with this today and came across this. Thanks for saving me a bunch of time!

Sign In or Register to comment.