Forms里设置左边ToolbarItem(iOS)

在Content中添加ToolbarItem 中默认都是显示在右边 ,现在需要第二个显示在左边,需要添加Custom Renderer 来实现

[assembly: ExportRendererAttribute(typeof(ContentPage), typeof(CustomContentPageRenderer))]
namespace AlertControllerDemo.iOS.Renderer
{
public class CustomContentPageRenderer : PageRenderer
{
    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);

        // Did this, because Xamarin.Forms couldn't set HasBackButton correct.
        // ViewController.ParentViewController.NavigationItem.SetHidesBackButton(!((ChangePasswordScreen)this.Element).HasBackButton, false);
        if (NavigationController == null)
            return;

        var navigationItem = NavigationController.TopViewController.NavigationItem;
        var leftNativeButtons = (navigationItem.LeftBarButtonItems ?? new UIBarButtonItem[] { }).ToList();
        var rightNativeButtons = (navigationItem.RightBarButtonItems ?? new UIBarButtonItem[] { }).ToList();

        if (rightNativeButtons.Count > 1)
        {
            var nativeItem = rightNativeButtons.Last();
            rightNativeButtons.Remove(nativeItem);
            leftNativeButtons.Add(nativeItem);
        }

        navigationItem.RightBarButtonItems = rightNativeButtons.ToArray();
        navigationItem.LeftBarButtonItems = leftNativeButtons.ToArray();
    }
}
}

效果:


你可能感兴趣的:(Forms里设置左边ToolbarItem(iOS))