views:

490

answers:

2

Hi,

I want to customize my UIBarButtonItem's appearance. Here's the code I'm currently using:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(267, 6, 48, 30)];
[editButton setCustomView:button];  // editButton is the UIBarButtonItem

Unfortunately, the UIButton does not get drawn, i.e. it's 100% transparent. I know the button is there because when I add a line to the code, like so:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(267, 6, 48, 30)];
[editButton setCustomView:button];  // editButton is the UIBarButtonItem
UIRectFill(button.frame);

I see a black square appearing in the position where the button should appear.

How can I get my UIButton to be drawn? I already looked at a ton of places, but nothing seems to work ...

A: 

I don't think you can set the custom view of a button to another button. Buttons are complex objects and nesting them like that usually won't work.

The custom view attribute is supposed to be for decoration, not functionality. You should load a custom view with appearance you want for the UIBarButtonItem and let it handle the actual button logic.

TechZen
Already tried it with a UIImageView, doesn't work, it still just vanishes.
A: 

Did you try to init it with a Custom view that is a UIImageView ? If it works you could create the UIImageView in such a way that it fakes a classic UIButton as described here :

http://stackoverflow.com/questions/1427818/iphone-sdk-creating-a-big-red-uibutton

But more generally, from a UI perspective, it's strange to have classic "UIButton" at the place of "UIBarButtonItem" :/

yonel
Tried it, same thing: the button just vanishes.