Jörn
Jörn

Reputation: 555

UIBarButtonItem with Custom View

I try to create a UIBarButtonItem with a custom view for a Toolbar. The view itself is displayed well, but when i click the BarButton, no action occurs. It looks like the touch event is not forwarded from the view to the UIBarButtonItem instance. I have checked the responder chain and i think it looks good. I have also searched the internet and checked the Apple documentation, but can't find any hint for my problem.

Here is my code:

   g__objWeatherButton = new UIBarButtonItem[1];

   UIView l__objCustomView = g__objWeatherDisplay.InfoBarButton; // Returns a reference to my custom view

   UIBarButtonItem l__objButton = new UIBarButtonItem(l__objCustomView);

   l__objButton.Clicked += delegate {this.WeatherButtonEvent();}; // my action handler
   l__objButton.Width = 200;
   l__objButton.Enabled = true;

   g__objWeatherButton[0] = l__objButton;

   this.Items = g__objWeatherButton; // "this" is my UIToolbar object

Can someone give me a hint where the problem is? Or a working code sample (in c# please - have found some examples in Objective-C, but apparently overlooked the crucial trick ;-)

Upvotes: 1

Views: 2583

Answers (3)

user1689394
user1689394

Reputation: 331

There is an easier way and the custom button will now react like a regular toolbar button.

 this.SetToolbarItems( new UIBarButtonItem[] {
            new UIBarButtonItem(UIBarButtonSystemItem.Refresh, (s,e) =>      {
                Console.WriteLine("Refresh clicked");
            })

            , new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace) { Width = 50 }
            , new UIBarButtonItem(UIImage.FromBundle
                                  ("wrench_support_white.png"), UIBarButtonItemStyle.Plain, (sender,args) => {
                Console.WriteLine("Support clicked");
            })
        }, false);

Upvotes: -1

Paul Sinnema
Paul Sinnema

Reputation: 2792

Although the answer put me in the right direction I still had trouble displaying the button. Only after I added the following the was button display with image.

l__objCustomUIButton.Frame = new System.Drawing.RectangleF(0, 0, 40, 40);

Regards Paul

Upvotes: 3

Dimitris Tavlikos
Dimitris Tavlikos

Reputation: 8170

No special trick. When you want to add a custom view to a toolbar or navigation bar, you should subscribe (and respond) to that view's events. So instead of using a UIView to hold your image, create a UIButton with UIButtonType.Custom and subscribe to that button's TouchUpInside event.

You then initialize it like you do with the UIView:

UIButton l__objCustomUIButton = UIButton.FromType(UIButtonType.Custom);
//l__objCustomUIButton.SetImage(UIImage.FromFile("your button image"), UIControlState.Normal);
l__objCustomUIButton.TouchUpInside += delegate { this.WeatherButtonEvent(); };
UIBarButtonItem l__objButton = new UIBarButtonItem(l__objCustomUIButton);

Just make sure you declare the button in the class scope.

Upvotes: 6

Related Questions