Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UIBarButtonItem to navigation Bar in Xamarin IOS

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

like image 547
kiran Avatar asked Sep 02 '14 11:09

kiran


1 Answers

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;
like image 157
Prashant Chaudhary Avatar answered Nov 14 '22 05:11

Prashant Chaudhary