Friday, July 17, 2009

[iPhone] add info button to UIToolBar


How can I insert 'info button' to UIToolBar ?

You should use 'UIBarButtonItem' to add some item to UIToolBar.

In example you can create it like below code.

UIBarButtonItem *actionItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];


You can use UIBarButtonSystemItem* but there is no info button.

I noticed there is other init method named 'initWithCustomView'
You can create customized BarButtonItem with it.

I created info button with UIButton like below code.

UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];


[button addTarget:self action:@selector(infoPressed:) forControlEvents:UIControlEventTouchUpInside];


UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithCustomView:button];

Let's do it !! :)



3 comments:

  1. Great! It's workign :) Thanks!

    ReplyDelete
  2. You could also do:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];

    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithImage:button.currentImage style:UIBarButtonItemStylePlain target:self action:@selector(onSearchButtonPressed:)];

    That way you will also get the "normal" bar button look...

    ReplyDelete
    Replies
    1. That is definitely not how it's supposed to look...

      Delete