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,
You can add a method to UIBarButtonItem without subclassing it using custom category:
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.
Another simple solution is
I found it this ways easy. It is sugested on top. "random.png" has to be in project. Just drag and drop any image.
Ok that category works very good because there are no problems with Popovercontroller :-)
Check this out simple solution.
Here 1x1.png is a 1 pixel transparent png image which you can download from the link below
http://commons.wikimedia.org/wiki/File:1x1.png