views:

1469

answers:

2

Hello,

I want to create a UIBarButtonItem with a custom image, but I don't want the border that iPhone adds, as my Image has a special border.

It's the same as the back button but a forward button.

This App is for an inHouse project, so I don't care if Apple reject or approves it or likes it :-)

If I use the initWithCustomView:v property of the UIBarButtonItem, I can do it:

UIImage *image = [UIImage imageNamed:@"right.png"];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
[button setBackgroundImage: [[UIImage imageNamed: @"right_clicked.png"] stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateHighlighted];

 button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);

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

UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];

[v addSubview:button];

UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithCustomView:v];

self.navigationItem.rightBarButtonItem= forward;

[v release];
[image release];

This works, but if I have to repeat this process in 10 views, this is not DRY.

I suppose I have to subclass, but what ?

  • NSView ?
  • UIBarButtonItem ?

thanks,

regards,

A: 

You can add a method to UIBarButtonItem without subclassing it using custom category:

@interface UIBarButtonItem(MyCategory)

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action;

@end

@implementation UIBarButtonItem(MyCategory)

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{
 // Move your item creation code here
}
@end

So anywhere in your code you can create bar item calling this method (provided that you include a header with its declaration).

P.S. You do not need to use 'v' UIView as you can create UIBarButtonItem with a button as custom view directly.
P.P.S. You also need [forward release] in your code.

Vladimir
PS => yes, it works also, less code :-)
mongeta
PPS => thanks, now added
mongeta
The custom category:I have to create the header file with those declarations, and the implementation file, where I put the code you're refering ? thanks
mongeta
ok, I've done it and it works perfectly ... thanks!
mongeta
A: 

Hi,

Just cam across your article - exactly what i need but must be missing something?

I set up the .m file as;

import "UIBarButtonItem.h"

@implementation UIBarButtonItem(MyCategory)

  • (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{ // Move your item creation code here

    UIImage *_image = [UIImage imageNamed:@"btn_HR_homex40.png"]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage: [_image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal]; //[button setBackgroundImage: [[UIImage imageNamed: @"right_clicked.png"] stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateHighlighted];

    button.frame= CGRectMake(0.0, 0.0, _image.size.width, _image.size.height); [button addTarget:@"tt://launcher" action:@selector(openURL:) forControlEvents:UIControlEventTouchUpInside];

    //self.navigationItem.rightBarButtonItem = button;

} @end

And imported the .h file in the view controller but how do i call this?

Thanks, Mike

Mike Barlow
Please (for the love of God) format your code **as `code`**.
Jonathan Sterling