How can I set the RightBarButtonItem Image on a custom PageRenderer for iOS?

Hello, I implemented a custom PageRenderer to be able to show the original image on the right side of the navigation bar (UIImageRenderingMode.AlwaysOriginal). I was able to do it successfully for all pages except the children of a TabbedPage (see attachments, all but the third screenshot are ok).

Here's my code:

using UIKit;

using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;

//[assembly: ExportRenderer(typeof(NavigationPage), typeof(MBank.iOS.CustomNavigationRenderer))]
[assembly: ExportRenderer(typeof(ContentPage), typeof(MBank.iOS.CustomPageRenderer))]

namespace MBank.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}

public class CustomPageRenderer : PageRenderer
{
    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);

        if (NavigationController != null)
        {
            if (NavigationController.TopViewController != null)
            {
                if (NavigationController.TopViewController.NavigationItem != null)
                {
                    foreach (var item in NavigationController.TopViewController.NavigationItem.RightBarButtonItems)
                    {
                        item.Image = item.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
                    }
                }
            }
        }
    }
}

/*
public class CustomNavigationRenderer : NavigationRenderer
{

    public override void PushViewController(UIKit.UIViewController viewController, bool animated)
    {
        base.PushViewController(viewController, animated);

        foreach (var item in TopViewController.NavigationItem.RightBarButtonItems)
        {
            item.Image = item.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
        }
    }
}
*/

}

The ViewWillAppear event gets called (I set a breakpoint using debug) and sets the new Image, even when displaying a Tabbed Page child. However, when the page being rendered is a displayed by clicking on the tab button (the one at the bottom of the page), the image gets tinted.

I also tried using a custom NavigationRenderer, but, the event does not even get called when I click on any other children of the Tabbed Page.

I welcome your suggestions and thank you in advance for your time.
-Juan Gabriel

Sign In or Register to comment.