Created
February 22, 2015 04:44
-
-
Save alexlau811/f1fff9e726333e6b4a2f to your computer and use it in GitHub Desktop.
Move toolbar items with priority = 0 to LeftBarButtonItems on iOS with Xamarin Forms. Originally written by Murally at http://forums.xamarin.com/discussion/21004/navigation-bar-left-toolbar-button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: ExportRenderer(typeof(ContentPage), typeof(ExtendedPageRenderer))] | |
public class ExtendedPageRenderer : PageRenderer | |
{ | |
public override void ViewWillAppear(bool animated) | |
{ | |
base.ViewWillAppear(animated); | |
var contentPage = this.Element as ContentPage; | |
if (contentPage == null || NavigationController == null) | |
{ | |
return; | |
} | |
var itemsInfo = contentPage.ToolbarItems; | |
var navigationItem = this.NavigationController.TopViewController.NavigationItem; | |
var leftNativeButtons = (navigationItem.LeftBarButtonItems ?? new UIBarButtonItem[] { }).ToList(); | |
var rightNativeButtons = (navigationItem.RightBarButtonItems ?? new UIBarButtonItem[] { }).ToList(); | |
rightNativeButtons.ForEach(nativeItem => | |
{ | |
// [Hack] Get Xamarin private field "item" | |
var field = nativeItem.GetType().GetField("item", BindingFlags.NonPublic | BindingFlags.Instance); | |
if (field == null) | |
{ | |
return; | |
} | |
var info = field.GetValue(nativeItem) as ToolbarItem; | |
if (info != null && info.Priority != 0) | |
{ | |
return; | |
} | |
rightNativeButtons.Remove(nativeItem); | |
leftNativeButtons.Add(nativeItem); | |
}); | |
navigationItem.RightBarButtonItems = rightNativeButtons.ToArray(); | |
navigationItem.LeftBarButtonItems = leftNativeButtons.ToArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about this such as render for Android ? is possible to Arranged toolbar item to Left even order as secondary ?