How to change background image of UIBarButtonItem create with interface builder - iphone

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 btnDone object- as first you assign it using IBOutlet and then de-reffer by using btnDone = [[UIBarButtonItem alloc] init...].. what you should do is add it programmatically instead of using IBOutlet. it will do your work.
Thanks,

Related

How to create multiline backBarButtonItem in UINavigationBar

I want to create multiline BackBarButtonItem. right now my back button display like this,
but i want to display it like this, color does not matter,
How can I do that? And this is my default back button which added while i am pushing my childviewcontroller thru my parentviewcontroller so i dont want to remove it.
You can do it using custom image,UILabel(MSLabel),UIButton.
MSLabel is used to change space between two line of UILabel. Because UILabel Does not provide any property to do that.
After you add MSLabel in your project use this code. Recommend size for backbtn.png is 48*30.
#import "MSLabel.h"
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the custom back button
UIImage *buttonImage = [[UIImage imageNamed:#"backbtn.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
lbl.text = #"Account Summary";
lbl.backgroundColor = [UIColor clearColor];
//lbl.font = [UIFont fontWithName:#"Helvetica" size:12.0];
lbl.numberOfLines = 0;
lbl.lineHeight = 12;
lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
lbl.font = [UIFont fontWithName:#"Helvetica" size:12];
[button addSubview:lbl];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 70, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
}
-(void)back {
NSLog(#"Dilip Back To ParentViewController");
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
Outcome will display like this, Use your image for better result.
In this case you should use custom button and use background image on this button.Because i think it may not be possible.See for custom button
UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10.0f,6.5f,60.0f,30.0f)];
[backBtn setBackgroundImage:[UIImage imageNamed:#"accountSummary"] forState:UIControlStateNormal];
[backBtn setBackgroundImage:[UIImage imageNamed:#"accountSummary"] forState:UIControlStateHighlighted];
[backBtn addTarget:self action:#selector(accountSummaryBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
[self.navigationItem setLeftBarButtonItem:leftButton];
- (void)accountSummaryBtnPressed:(id)sender
{
//--------- do stuff for account summary
}
What i believe you can Create CustomView with ImageView And Transparent label with numberOflines more than two or whatever you want to restrict it to , then replace the navigation back button with That Barbutton initiated with CustomVIew, That Can Solve your problem,, Hope fully

Problems with BackButtonItem

Well, I have a NavigationController with a diferent color and I want the back button to also have a background image, a highlighted background image, and a custom text color. How can I do this? I have tried lots of things but I havent been able to change this backbutton.
I guess you have to create your own custom button.
Create a custom UIBarButtonItem that suits your requirement.
Assign this Custom UIBarButtonItem to the leftBarButtonItem of the NavigationItem.
You can only point to the leftBarButtonItem, since the backBarButtonItem is ReadOnly.
self.navigationItem.leftBarButtonItem = aCustomBarButtonItem;
self.navigationItem.hidesBackButton = YES;
Hope this helps...
EDIT
Code that can help :
UIButton *aCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aCustomButton setBackgroundImage:[UIImage imageNamed:#"back1.png"] forState:UIControlStateNormal];
[aCustomButton setBackgroundImage:[UIImage imageNamed:#"back2.png"] forState:UIControlStateHighlighted];
[aCustomButton addTarget:self action:#selector(onClickOfBack) forControlEvents:UIControlEventTouchUpInside];
[aCustomButton setFrame:CGRectMake(0, 0, 50, 40)];
UIBarButtonItem *aCustomBarButton = [[UIBarButtonItem alloc] initWithCustomView:aCustomButton];
self.navigationItem.leftBarButtonItem = aCustomBarButton;
[aCustomBarButton release];

How to change the color of barButtonItem?

On a navigationBar as a rightBarButtonItem I take edit button by the following coding
self.navigationItem.rightBarButtonItem=self.editButtonItem;
but I want to change the color of edit button I mean I want to change the color of rightBarButtonItem. How can I do this?
plz reply as early as possible.
thanx in advance.
You will need to create a custom button and initialize your UIBarButtonItem using initWithCustomView.
Create your UIButton as you like if with your custom image and then:
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton];
You are constrained making standard UIBarButtonItem. But you can init it with any UIView of your choice, e.g. standard UIButton which you can color as you like.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 30, 20); // change as you like being constrained with navigation bar hieght
[myButton setTitle:#"My Button" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"Image for my button.png"] forState:UIControlStateNormal]; // <-- you can provide image instaed of title if you like
[myButton setBackgroundImage:[UIImage imageNamed:#"Background image for my button.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside]; // <-- don't forget configure this!!!
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
Visit this link. It is kind of same answer.
specially indicating or highlighting a toolbar button when a note is added in iphone
Usage of method [[UIBarButtonItem alloc] initWithCustomView:btnInfo]; can help.

Can't see my navigationbar custom button, but it is clickable

I want to add a custom button (to the right) in my navigation bar.
I have the following code in my "viewDidLoad" function :
UIButton *audioBtn = [[UIButton alloc] init];
[audioBtn setTitle:#"Play" forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"play.png"];
[audioBtn setBackgroundImage:buttonImage forState:UIControlStateNormal];
[audioBtn addTarget:self action:#selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;
[audioBtn release];
[button release];
I can't see the button in my navigation bar, but if I click to the right (where the button is supposed to be), it triggers the function "toggleAudioPlayback", so the only problem is that I don't see the button!
I tried with different image, setting a background color, nothing works...
By the way, I use this image somewhere else in the code, and I see it (on a custom button, but not in a navigationBar).
Help me please!
Make your life easier and instead of a customView just use a UIBarButtonItem with a custom image:
UIBarButtonItem *audioBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"play.png"] style:UIBarButtonItemStylePlain target:self action:#selector(toggleAudioPlayback:)];
self.navigationItem.rightBarButtonItem = audioBtn;
[audioBtn release];
UPDATE
Since you have indicated you want your button to not have a border, then you will need to use a CustomView afterall, but I have modified your code to make this work (the key difference is the assigning of the frame, which is set to the size of the image, but you could set it to a custom size to have the image centered):
UIButton *audioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *playImg = [UIImage imageNamed:#"play.png"];
[audioButton setBackgroundImage:playImg forState:UIControlStateNormal];
[audioButton setBackgroundImage:playImg forState:UIControlStateHighlighted];
audioBtn.frame = CGRectMake(0,0,playImg.size.width,playImg.size.height);
[audioBtn addTarget:self action:#selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;
[audioBtn release];
[button release];
Instead of [[UIButton alloc] init] try the +buttonWithType: method on UIButton.

UIButton as UINavigationbar button

I have a Tab bar application. and in one of the tab bars I have a navigation contorller.
Im trying to set a UIButton with an image as the right button the navigation bar.
UIButton *refreshButton = [[UIButton alloc] init];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refreshSection) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:refreshButton];
//[rightButton setImage:[UIImage imageNamed:#"refresh_icon.png"]]; << DOESN'T WORK EITHER
self.navigationItem.rightBarButtonItem = rightButton;
[refreshButton release];
[rightButton release];
but the image doesn't show up. I get an invisible button.
EDIT:
I want the button style to be plain or background to be transparent, so that only the image shows. Hence I am using a UIbutton instead of using a UIBarButton directly.
You don't have to create a UIButton and use it as custom view in the UIBarButtonItem. You can create a UIBarButtonItem directly with an image:
UIImage *myImage = [UIImage imageNamed:#"myImage.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:#selector(refreshSection)];
self.navigationItem.rightBarButtonItem = button;
[button release];
--
Updated, according to comments
Use a UIImageView as custom view in the UIBarButtonItem:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.rightBarButtonItem = rightButton;
[imageView release];
[rightButton release];
--
Updated
Tried the last approach and while this does visually look like you want it to, I can't seem to get it to handle taps. Although the target and action has been set.
--
Final update
I've gotten your initial code in the question up & running. The reason the image is not showing, is because you haven't set the frame of the button. Once you set the frame, the image is shown in the navigation bar.
Here's my snippet I used & tested:
UIImage *myImage = [UIImage imageNamed:#"paw.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
[myButton release];
Note: I have set the showsTouchWhenHighlighted property of the UIButton to visually hightlight the button when it is pressed.
I ended up doing this
UIButton *refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(292, 9, 27, 27)];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refresh) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:refreshButton];
[refreshButton release];
It works fine now but I am not really sure if this is the right way of doing it.