问题
How to add custom UIBarButtonItem to navigationbar rightside.
public UIBarButtonItem btn_Cart {get; set;}
public DetailedController (){
string buttonTitle = "One";
btn_Cart = new UIBarButtonItem (buttonTitle, UIBarButtonItemStyle.Bordered, null);
btn_Cart.Clicked += (s, e) => { new UIAlertView ("click!", "btnOne clicked", null, "OK", null).Show ();
};
}
public override void ViewDidLoad (){
base.ViewDidLoad ();
// Added btn_Cart to RightSide Of Naviation Bar.
this.NavigationItem.SetRightBarButtonItem (btn_Cart, true);
}
Its not added to navigationItem.
If I change my code to
public override void ViewDidLoad (){
this.NavigationItem.SetRightBarButtonItem(
new UIBarButtonItem(UIImage.FromBundle ("Image.png")
, UIBarButtonItemStyle.Plain
, (sender,args) => {
new UIAlertView ("click!", "btnOne clicked", null, "OK", null).Show ();
})
, true);
}
// Its Added to NavigationItem at right side.
public class DetailedTabBarController : UITabBarController {
public override void ViewDidLoad (){
base.ViewDidLoad ();
this.NavigationItem.SetRightBarButtonItem(
new UIBarButtonItem(UIImage.FromBundle ("/Images/Image.png"),
UIBarButtonItemStyle.Plain,
(sender,args) => {
Console.WriteLine("Do Some Action!");
}), true);
}
}
I do not want to add like this.
Can any one advice me to added UIBarButtonItem to NavigationItem at right side. @All Thanks In Advance
回答1:
First Create a UIBarButtonItem. Then add it to NavigationItem. here is sample code
UIBarButtonItem btn = new UIBarButtonItem ();
btn.Image = UIImage.FromFile("Image.gif");
btn.Clicked += (sender , e)=>{System.Diagnostics.Debug.WriteLine("Button tap");};
NavigationItem.RightBarButtonItem = btn;
来源:https://stackoverflow.com/questions/25622434/add-uibarbuttonitem-to-navigation-bar-in-xamarin-ios