在导航控制器上添加左栏按钮

Adding left barbutton on navigation controller

我正在尝试在导航控制器中添加左栏按钮,但它不起作用。我的代码:

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";

我使用了 this url 中的代码。 hamburger_menu 是我的资产中存在的图像。

如果您想将左栏按钮添加到特定的 UIViewController。我们不应该设置它的 NavigationController 的 NavigationItem。

请注意每个 UIViewController 都有一个 NavigationItem 属性。设置自己的 NavigationItem 来实现你的效果:

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

另外因为UINavigationController是UIViewController的子类,所以可以设置它的NavigationItem。但是这个项目不是你想要的。

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