I need custom icons in toolbar (black icons on yellow toolbar background). I tried UIBarButtonItem initWithImage constructor, but it this case is icon displayed using alpha values, and it seems that there is no way to change basic whi开发者_C百科te icon color. I ended up using UIButton, but it will be much better, to just change default icon color, is it possible?
UIImage *buttonImage = [UIImage imageNamed:iconName];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithImage:buttonImage style:UIBarButtonItemStylePlain target:nil action:nil];
You can make the toolbar yellow by setting the tint:
toolbar.tintColor = [UIColor colorWithRed:0.83 green:0.43 blue:0.57 alpha:0.5];
If you want custom UIBarButtonItem with custom image, colors etc... like you get in UIButton, one option is to create a class which encapsulates UIButton as the custom view in UIBarButtonItem. Here's my custom class - hope it helps:
@interface ENBarButtonImageItem : UIBarButtonItem
{
UIButton *_button;
}
@implementation ENBarButtonImageItem
- (id)initWithFrame:(CGRect)frame
image:(UIImage*)image
backgroundImage:(UIImage*)bgImage
{
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setFrame:frame];
self = [super initWithCustomView:_button];
if (self)
{
if (image)
[_button setImage:image forState:UIControlStateNormal];
if (bgImage)
[_button setBackgroundImage:bgImage forState:UIControlStateNormal];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
image:(UIImage*)image
backgroundImage:(UIImage*)bgImage
target:(id)target
action:(SEL)selector
{
self = [self initWithFrame:frame image:image backgroundImage:bgImage];
if (self)
{
[_button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)dealloc
{
[super dealloc];
[_button release];
}
- (void)addTarget:(id)target action:(SEL)selector forControlEvents:(UIControlEvents)controlEvents
{
[_button addTarget:target action:selector forControlEvents:controlEvents];
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
[_button setImage:image forState:state];
}
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
{
[_button setImage:image forState:state];
}
Read description of parameters of initWithImage:style:target:action:
image
The item’s image. If nil an image is not displayed.
The images displayed on the bar are derived from this image. If this image is too large to fit on the bar, it is scaled to fit. Typically, the size of a toolbar and navigation bar image is 20 x 20 points. The alpha values in the source image are used to create the images—opaque values are ignored.
Everything except alpha is ignored. Toolbar is black and white.
Subclass or use other UI frameworks, like three20 if you really need colors there.
精彩评论