1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-26 12:16:07 +01:00

custom content page renderer for left nav items

This commit is contained in:
Kyle Spearrin 2019-06-11 20:57:12 -04:00
parent 17060a2a1b
commit b6e7db6ecf
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Bit.iOS.Renderers;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomContentPageRenderer))]
namespace Bit.iOS.Renderers
{
public class CustomContentPageRenderer : PageRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if(!(Element is ContentPage contentPage) || NavigationController == null)
{
return;
}
var navigationItem = NavigationController.TopViewController.NavigationItem;
var leftNativeButtons = (navigationItem.LeftBarButtonItems ?? new UIBarButtonItem[] { }).ToList();
var rightNativeButtons = (navigationItem.RightBarButtonItems ?? new UIBarButtonItem[] { }).ToList();
var newLeftButtons = new List<UIBarButtonItem>();
var newRightButtons = new List<UIBarButtonItem>();
foreach(var nativeItem in rightNativeButtons)
{
// Use reflection to get Xamarin private field "_item"
var field = nativeItem.GetType().GetField("_item", BindingFlags.NonPublic | BindingFlags.Instance);
if(field == null)
{
return;
}
if(!(field.GetValue(nativeItem) is ToolbarItem info))
{
return;
}
if(info.Priority < 0)
{
newLeftButtons.Add(nativeItem);
}
else
{
newRightButtons.Add(nativeItem);
}
}
foreach(var nativeItem in leftNativeButtons)
{
newLeftButtons.Add(nativeItem);
}
navigationItem.RightBarButtonItems = newRightButtons.ToArray();
navigationItem.LeftBarButtonItems = newLeftButtons.ToArray();
}
}
}

View File

@ -112,6 +112,7 @@
<Compile Include="Main.cs" />
<Compile Include="AppDelegate.cs" />
<Compile Include="Migration\KeyChainStorageService.cs" />
<Compile Include="Renderers\CustomContentPageRenderer.cs" />
<Compile Include="Renderers\HybridWebViewRenderer.cs" />
<Compile Include="Services\DeviceActionService.cs" />
<Compile Include="Services\iOSPushNotificationHandler.cs" />