ToolBar and buttons have custom images
- (void)viewDidLoad {
[super viewDidLoad];
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)];
[myToolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolbar_40.png"]] autorelease] atIndex:0];
UIBarButtonItem *barButtonDone = [self createImageButtonItemWithNoTitle:@"button_down.png" target:self action:@selector(closeMe:)];
UIBarButtonItem *barButtonShare = [self createImageButtonItemWithNoTitle:@"button_share.png" target:self action:@selector(actionShareButton:)];
UIBarButtonItem *barButtonFlexibleGap = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
myToolbar.items = [NSArray arrayWithObjects:barButtonDone,barButtonFlexibleGap,barButtonShare,nil];
[self.view addSubview:myToolbar];
[myToolbar release];
}
-(void)closeMe:(id)sender
{
NSLog(@"closeMe");
}
-(void)actionShareButton:(id)sender
{
NSLog(@"actionShareButton");
}
-(UIBarButtonItem *)createImageButtonItemWithNoTitle:(NSString *)imagePath target:(id)tgt action:(SEL)a
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [[UIImage imageNamed:@"button_slice.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
UIImage *buttonPressedImage = [[UIImage imageNamed:@"button_slice_over.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
CGRect buttonFrame = [button frame];
buttonFrame.size.width = 32;
buttonFrame.size.height = 32;
[button setFrame:buttonFrame];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView.image = [UIImage imageNamed:imagePath];
[button addSubview:imageView];
[imageView release];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
[button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return [buttonItem autorelease];
}
These png files need to be added
- toolbar_40.png for toolbar image
- button_down.png for button image
- button_share.png for button image
- button_slice.png for button background
- button_slice_over.png for button background (pressed)