Xamarin.iOS中的UINavigationBar自定义右键按钮

时间:2016-05-10 14:52:31

标签: xamarin uinavigationcontroller xamarin.ios uinavigationbar

我在Xamarin.iOS的UINavigationBar右侧添加了带图片的自定义按钮, 但遗憾的是还没有成功。问题是它总是显示一个蓝色背景的按钮。 这是我的代码:

    var customBtn = new UIBarButtonItem ();
    customBtn.Image = UIImage.FromFile ("home.png");
    customBtn.Clicked += (object sender, EventArgs e) => 
    {
        Console.WriteLine("This button is clicked");
    };

    NavigationItem.RightBarButtonItem = customBtn;

1 个答案:

答案 0 :(得分:0)

认为您需要使用SetRightBarButtonItems,如此:

var rightButton = new UIButton (UIButtonType.Custom);
rightButton.SetImage (UIImage.FromBundle ("home"), UIControlState.Normal);
rightButton.SetImage (UIImage.FromBundle ("home"), UIControlState.Disabled);
rightButton.Frame = new CGRect (0, 0, 17, 17); // set this to the size of the image

var rightBarButton = new UIBarButtonItem (rightButton);
navItem.SetRightBarButtonItems(new UIBarButtonItem[] {rightBarButton }, false);
rightButton.TouchUpInside += (sender, e) => {
    Console.WriteLine("This button is clicked");
};