Add UIBarButtonItem to navigation Bar in Xamarin IOS

10,023

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;
Share:
10,023
kiran
Author by

kiran

Updated on June 14, 2022

Comments

  • kiran
    kiran almost 2 years

    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