Access UINavigationItem from within MAUI ContentPage (in order to add native buttons to navigationbar)

40 views Asked by At

I'm writing a MAUI app, in which I'd like to add native buttons to an iOS title bar. As I understand it, I need to call SetRightBarButtonItems on the top-most NavigationItem, which for a single-page app should be MainPage. I've tried adding the following code to MainPage.xaml.cs:

protected override void OnAppearing()
{
    #if IOS
    var settingsButton = new UIBarButtonItem() { 
        Title="Settings",
        Image = UIImage.GetSystemImage("gearshape")
    };
    var ni = Platform.GetCurrentUIViewController().NavigationItem;
    ni.Title = "TEST!!";
    ni.SetRightBarButtonItems([settingsButton],true);
    #endif
    base.OnAppearing();
}

But it doesn't seem to be getting the correct ViewController - or it doesn't reflect the changes correctly (even the title property doesn't work). It's getting a ViewController, which isn't the case if I try running this in AppShell.xaml.cs, but anything I do to it doesn't get reflected in the UI - what is it i'm missing here?

0

There are 0 answers