Add UIBarButtonItem to navigation Bar in Xamarin IOS

╄→гoц情女王★ 提交于 2020-02-21 12:48:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!