Add button (UIButtonBarItem) is not visible in navigationController? - iphone

Actually I added two button (UIButtonBarItem) and assigned it to to NavigationItem's rightbarbuttonitem but only edit button is visible and add button is not visible but when we click on it it gets invoked ?
chk it out my code http://pastebin.com/Ew4zPEUz

Try using this code in ViewDidload method
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,60,30);
[button setImage:[UIImage imageNamed:#"iPhoneBackButton.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem=barButtonItem;

finally solved this problem .
just removed below line
self.navigationItem.rightBarButtonItem.tintColor=[UIColor clearColor]
and its works :)

Related

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.

Add image into navigation bar

When a item is added to favorites. I would like to indicate that it has been successfully added to the user by having a image of a star to fade in, into the navigation bar in place of the right bar button. How can I do this?
Any help will be appreciated! Thanks
You can customize your right bar button item using custom button.
//custom back button
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(-2, 0, 52, 30)];
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = btnItem;
[btnItem release];

Setting border around custom UIBarButtonItem

I want to put a custom button as right navigation bar button item. Here is the code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"my.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(filterResult) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 50, 50)];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
The problem is that I can see only the image and not the button around it like it comes for UIBarButtonSystemItemAdd or other types.
What should I do?
[button setImage:[UIImage imageNamed:#"my.png"] forState:UIControlStateNormal];
replaces the entire image for the specified state. You have to draw the full own image and not only the icon on it (or whatever you want).

custom back button image does not work on the navigation bar

I created a custom navigation back button. But when I add this code, my back animation doesn't work and it goes back with animation. Why? I think it depends on my #selector action.
- (void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"goback.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(//HERE ! i don't know put what thing !) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
ok i created a function and works fine :D :
-(void)pushNav {
[self.navigationController popViewControllerAnimated:YES];
}