Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MasterDetail + Right-to-Left in Xamarin.Forms v3

Tags:

I'm using new forms feature Right-to-Left, it works well except MasterDetail hamburger menu icon. It stays on the left side and I need to move it to right whem localization is changed. Any ideas or could somebody help me with custom renderer?

like image 533
Milan MiNo Michalec Avatar asked May 17 '18 13:05

Milan MiNo Michalec


1 Answers

well not impossible but some dirty coding is needed:

please check the solution here

as recap: To force the layout RTL and LTR on IOS:

1- Create this class/service

using System;
using System.IO;
using Xamarin.Forms;
using yourproject.iOS;
using yourproject.database;
using ObjCRuntime;
using System.Runtime.InteropServices;
using UIKit;
using System.Diagnostics;

[assembly: Dependency(typeof(PathManager_IOS))]
namespace yourproject.iOS
{
 public class PathManager_IOS : IPathManager
 {
[DllImport(ObjCRuntime.Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]
internal extern static IntPtr IntPtr_objc_msgSend(IntPtr receiver, IntPtr selector, UISemanticContentAttribute arg1);

    public PathManager_IOS()
    {

    }
   public void SetLayoutRTL()
    {
        try
        {
            Selector selector = new Selector("setSemanticContentAttribute:");
            IntPtr_objc_msgSend(UIView.Appearance.Handle, selector.Handle, UISemanticContentAttribute.ForceRightToLeft);
        }
        catch (Exception s)
        {
            Debug.WriteLine("failed to set layout...."+s.Message.ToString());
        }
    }
    public void SetLayoutLTR()
    {
        try
        {
            Selector selector = new Selector("setSemanticContentAttribute:");
            IntPtr_objc_msgSend(UIView.Appearance.Handle, selector.Handle, UISemanticContentAttribute.ForceLeftToRight);
        }
        catch (Exception s)
        {
            Debug.WriteLine("failed to set layout...." + s.Message.ToString());
        }
    }
 }
} 

ps: please change "yourproject" to your project name...

To Call this on startup

in AppDelegate.cs

   PathManager_IOS pathManager = new PathManager_IOS();
   if (lang == 3)
    {
      pathManager.SetLayoutRTL();/* RTL */

    }
    if (lang == 1||lang == 2)
    {
       pathManager.SetLayoutLTR();/*   LTR   */
    }
    LoadApplication(new App(m, lang));

TO call this from the PCL shared pages or project

/* arabic */
DependencyService.Get<IPathManager>().SetLayoutRTL();

/* English */
DependencyService.Get<IPathManager>().SetLayoutLTR();

English - hamburger menu icon on the LEFT. ARABIC - hamburger menu icon on the RIGHT.

Don't forget to set the flow direction on language change

if(lang==3)
                {//arabic 
                    this.FlowDirection = FlowDirection.RightToLeft;
                    this.Master.FlowDirection= FlowDirection.RightToLeft;
                    this.Detail.FlowDirection= FlowDirection.RightToLeft;

                } 

hope this helps! all this for that hamburger icon !!!

Cheers, Rabih

like image 123
Rabih harb Avatar answered Sep 28 '22 17:09

Rabih harb