简体   繁体   中英

Adding left barbutton on navigation controller

I am trying to add the left bar button in navigation controller but it is not working. My code:

this.NavigationController.NavigationBar.TintColor = UIColor.White;
        this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGBA(204,165,99,255);
        this.NavigationController.NavigationItem.SetLeftBarButtonItem(
            new UIBarButtonItem(UIImage.FromFile("hamburger_menu")
            , UIBarButtonItemStyle.Plain
            , (sender, args) => {
                // button was clicked
            })
        , true);

        this.Title = "Dashboard";

I used code from this url. hamburger_menu is an image present in my Assets.

If you want to add the left bar button to a particular UIViewController. We shouldn't set its NavigationController's NavigationItem.

Be aware of that every UIViewController has a NavigationItem property. Set its own NavigationItem to achieve your effect:

this.NavigationItem.SetLeftBarButtonItem(
    new UIBarButtonItem(UIImage.FromBundle("hamburger_menu")
    , UIBarButtonItemStyle.Plain
    , (sender, args) => {
    // button was clicked
})
, true);

Moreover because UINavigationController is a subClass of UIViewController, you can set its NavigationItem. But this item is not what you want.

我正在使用UIImage.FromFile读取图像,但是当我将其更改为UIImage.FromBundle时,它开始工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM