I create a few UIBarButtonItem with interface builder and try to customize the button to green color. While I couldn’t find ways to change background color of the button, I decide to code in this way:
IBOutlet UIBarButtonItem *btnDone;
-(void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [[UIImage imageNamed:@"btnGreen.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[doneButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[doneButton setTitle:@"Done" forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(saveDateEdit:) forControlEvents:UIControlEventTouchUpInside];
[doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
doneButton.frame = CGRectMake(0.0, 0.0, 50, 30);
btnDone = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
[doneButton release];
}
I create IBOutlet and link to the button from interface builder while I try to override this during it first load. However, it is showing the default button.
Would like to know why is this happening? Or even better someone can suggest me better way changing my UIBarButtonItem to green.
Appreciate your help!
You are missing one point,
You are de-referring the
btnDoneobject- as first you assign it usingIBOutletand then de-reffer by usingbtnDone = [[UIBarButtonItem alloc] init...].. what you should do is add it programmatically instead of usingIBOutlet. it will do your work.Thanks,