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];
}
Related
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 :)
I have made at least 10 buttons for my navigation bar, but it never seems to work right.
The rounded edges get pixelated. I cant have that in an app, so can anyone tell me how to make a good icon that looks like an apple one? Also what is the proper size? The code in the app for the button is
UIButton *backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
backbtn.frame = CGRectMake(0, 0, 55, 30);
[backbtn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[backbtn addTarget:self action:#selector(goBackOne) forControlEvents:UIControlEventTouchUpInside]; forState:UIControlStateNormal ];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];//set new button
self.navigationItem.hidesBackButton = YES;//hide original back button
Try this code:
UIImage *backButtonImage = [UIImage imageNamed:#"backButton.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBackBarItem;
and, the back method:
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
The code above just set the image for normal state. You can also set highlighted state for a better appearance. Add some codes Like:
UIImage *backButtonImageHighlighted = [UIImage imageNamed:#"backButtonHighlighted.png"];
[backButton setImage:backButtonImageHighlighted forState:UIControlStateHighlighted];
I have this code that changes the back button of my UINavigationBar
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:#"backag.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"selback.png"] forState:UIControlStateHighlighted];
button.adjustsImageWhenDisabled = NO;
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 30, 30);
[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;
// Cleanup
[customBarItem release];
If I put it in the viewDidLoad method it works fine. However, when I load the next view the old style button shows up. To fix this I tried putting this code in the next view's viewDidLoad method, but then no button is visible.
Any ideas as to what might be causing this?
Thanks!
Apply same code in
-(void)viewDidAppear:(BOOL)animated
{
}
I used tis code in both view and it's worked fine...
// add following code in your both view's viewDidLoad method...
UIButton *leftButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton1 setImage:[UIImage imageNamed:#"Viewone.png"] forState:UIControlStateNormal];
leftButton1.frame = CGRectMake(0, 0, 30, 30);
[leftButton1 addTarget:self action:#selector(yourclickevent) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton1];
[leftButton1 release];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:[UIImage imageNamed:#"ViewSecond.png"] forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, 30, 30);
[leftButton addTarget:self action:#selector(yourclickevent) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
[leftButton release];
Hope,this will help you...enjoy..
You need to ensure you set the hidesBackButton property on the navigationItem:
// Setup custom back button
self.navigationItem.hidesBackButton = YES;
I want to disable the border around iPhone SDK's add button, so it's just the "+" button (no black bg):
This seems to be a simple task when it's located in the toolbar, but not when it's located in the UINavigationBar. Anyways, if anyone knows how, or if it's even possible, then please share! Here's my current code in my RootViewController.m:
self.title = #"Code Master";
addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(addButtonClicked:)];
self.navigationItem.rightBarButtonItem = addButton;
EDIT
#Dee so your code works great, displays exactly what I want! There is another problem now that I'll explain:
Edit Button: Left side of UINavigationBar
Done Button: Left side of UINavigationBar (hidden); when the Edit button is clicked, it replaces the Edit Button with this "Done" button
Plus Button: Right side of UINavigationBar; hides when in "edit mode", shows when in "normal mode (when Done button is not visible)"
So basically my problem now is that whenever I click Edit, then Done, the plus button doesn't return from hiding. I'm using your exact code and it worked with my previous code. I'm pretty sure that it's because in your code, theres nothing labelling it "addButton". Here's the code later on in my main file:
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:YES];
if (editing)
{
self.navigationItem.rightBarButtonItem = nil;
}
else
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Plus.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(addButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 50, 29)];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = barBtn;
[barBtn release];
}
[self.tableView reloadData];
}
Create "+" image and set it to button & give that button to UIBarButtonItem like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Plus.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(addButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 50, 29)];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barBtn];
[barBtn release];
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).