I am trying to add leftBarButtonItem to my iPhone app. I have Navigation controller and inside it TableViewController.
I am coding the following line in my AppDelegate.m file.
self.viewCon = [[UITableViewController alloc]init];
self.navCon = [[UINavigationController alloc]initWithRootViewController:self.viewCon];
self.viewCon.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"Second View" style:UIBarButtonItemStyleBordered target:self action:#selector(push)]autorelease];
The navigation item does not appear on the screen. The table is on. What might be the prob?
By the way, no ARC or storyboard.
I'm assuming you want a TableView with a NavigationController on top of it, in which case your root viewcontroller would be your navigation controller and not your tableview. If you built the app in IB properly (stab in the dark since I can't see what you did), then you should not have to initialize your navigation controller. If this is the case then it should be as simple as
self.viewCon.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Second View" style:UIBarButtonItemStyleBordered target:self action:#selector(push)]autorelease];
What I think is happening is your are overwriting your currently set up NavController with your programmatic initialization which is causing everything to disappear.
If all this is the case and you may have tried this, post back the error and a screen shot of your IB setup.
Try this instead:
UIBarButtonItem *bbi = [[[UIBarButtonItem alloc] initWithTitle:#"Second View" style:UIBarButtonItemStyleBordered target:self action:#selector(push)]autorelease];
[self.navigationItem setLeftBarButtonItem:bbi];
[self.navigationController setNavigationBarHidden:NO];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setFrame:CGRectMake(0, 0, 49,32)];
[ backButton setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = back;
[back release];
Related
I'm writing an app in objective C for ios 8, and my navigation bar has an annoying margin thats causing it to be shifted to the right ~20 pixels for leftBarButtonItem and ~20 left for rightBarButtonItem, as shown in the image below. I have tried using negative spacers per some other posts to no avail. Any ideas?
(I'm trying to do this without using storyboards or xibs.)
Here is the code:
UIToolbar* toolbar_temp = [[UIToolbar alloc] init];
toolbar_temp.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45);
toolbar_temp.barTintColor= [UIColor blackColor];
NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIImage *camimage = [UIImage imageNamed:#"cancel"];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:camimage style:UIBarButtonItemStylePlain target:self action:nil];
[items addObject:spaceItem ];
[items addObject:customItem ];
[items addObject:spaceItem ];
[toolbar_temp setItems:items animated:NO];
UIBarButtonItem *allButton = [[UIBarButtonItem alloc] initWithCustomView:toolbar_temp];
self.navigationItem.leftBarButtonItem = allButton;
EDIT:
I'm not sure if this gives any hints, but for some reason this works:
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
While this does not:
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
SOLUTION:
I ended up finding the solution myself. The issue was that I have a tabbar controller which had a navigation controller as one of the views. The navigation controller can only be setup in viewDidload, which doesn't get called in tabbar controllers. The solution was to add viewDidAppear and add the navigation controller within that function.
- (void)viewDidAppear:(BOOL)animated{[self addToolbar];}
Thanks all for the help!
It seems you cannot alter the minimum margins. You can do is add the toolbar_temp to the navigation bar:
[self.navigationController.navigationBar addSubview:toolbar_temp];
Or add to the self.view:
self.navigationController.navigationBarHidden = YES;
[self.view addSubview:toolbar_temp];
I want to change leftBarButton title. I've tried this
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setLeftBarButtonItem:newBackButton];
but it brings changes like
But I want it like
How it can be possible?
It may helpful for you
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = backBarButton;
From the view controller (ViewController1) that is pushing the next view controller (ViewController2), you need to set the backBarButtonItem.
#implementation ViewController1
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"New Title" style:UIBarButtonItemStylePlain target:nil action:nil];
}
It's totally normal that you don't see any arrow in your back button. That's because you're creating a new bar button that you set its title and its style. But the style you've set for your bar button item won't give you what you wanted.
I suppose that you're using UINavigationController to navigate. If it's the case, you need to get a reference to navigationBar that UINavigationController displays on the screen. Then, you'll be able to change its color via tint color property. So,
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
would do what you want to achieve. Here is the image i obtained using the code i mentioned above.
If you're not using UINavigationController, the simplest solution is to provide an image for your back button which has already an arrow.
You should set backBarButtonItem instead of leftBarButtonItem. And remember to set it not in current view controller (where you wish it to be displayed) but in parent one (to which this button will return you).
Use this Code and
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"BackBtn.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
and for going to back use this method .
-(void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
This worked for me
UIImage *image = [[UIImage imageNamed:#"btn_back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = button;
- (IBAction)backAction:(id)sender
{
MainMenuViewController *mainMenuViewController = [[MainMenuViewController alloc] init];
[self.navigationController pushViewController:mainMenuViewController animated:YES];
}
Here's my code for removing the back UIBarButtonItem of a navigation bar and replacing it with a cancel button:
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = cancelButtonItem;
Every example I've seen online doesn't hide the backButton before replacing it with a custom item. I may be wrong but it just seems like one unneeded line of code.
Did you try using UIBarButtonSystemItemCancel instead of UIBarButtonItemStylePlain in the style of your UIBarButtonItem?
Try this,
UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelAction)];
self.navigationItem.leftBarButtonItem = cancelButton;
Also if you have used self.navigationItem.backBarButtonItem in your parentViewController, it will show that button in the childViewController's leftBarButtonItem by default, if your custom leftBarButton in the childViewController is not added properly.
Try this.
Write this in your app delegates did finish launching.
LoginViewController *loginVc = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:loginVc];
self.window.rootViewController = self.navigationController;
self.navigationController.navigationBarHidden =YES;
// and use uibutton in your view controller at place of back button or cancel button
// and u can push or pop your view from there.
I have an existing code which was working fine in ios6. but in ios7 left most item("refresh button") is not showing align to other two UIBarButtonItem . its showing little down. here is the code for iOS6. what changes do i need to make this working in iOS7.
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard save button
UIBarButtonItem* refreshButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(refreshButtonClicked:)];
refreshButton.style=UIBarButtonItemStyleBordered;
//self.navigationItem.rightBarButtonItem = refreshButton;
[buttons addObject:refreshButton];
[refreshButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard delete button with the trash icon
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button addTarget:self action:#selector(InfoButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
infoItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[buttons addObject:infoItem];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar]autorelease];
Thanks
Try:
self.navigationItem.rightBarButtonItems = buttons;
I want to have a navigation bar with a settings button on it that stays there regardless of which tab it is on.
Sorry for the short message its late for me,
Chase
What you can do is drag a UIToolBar onto your storyboard controllers and set the buttons and the actions you want there. You could make all your controllers extend form the same base class as to not have to repeat your code, as well as instantiate this toolbar in code if you find it more pleasing.
The problem will be when it comes to using navigation as the navbar will replace/overlap with this toolbar.
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(myAction:)];
[myToolbar setItems:[NSArray arrayWithObjects:myButton, nil]];
[self.view addSubview:myToolbar];
In AppDidFinishLaunching:
Add Your TabBarcontroller (rootViewController) for UINavigationController
Now add setting button below is example in navigationBar
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonSystemItemDone target:nil action:nil];
navigationCOntroller.navigationItem.rightBarButtonItem = rightButton
rightButton will be visible throught application
If you just want to place a navigation bar with a settings button that can be accessed from any view using a UITabBarController you can do something like this in the AppDelegate -->
YourViewController1 *yourVC1 = [YourViewController1 alloc] initWithNibName:#"YourViewController1" bundle:nil];
UINavigationController *yourNVC1 = [[UINavigationController alloc] initWithRootViewController:yourVC1];
YourViewController2 *yourVC2 = [YourViewController2 alloc] initWithNibName:#"YourViewController2" bundle:nil];
UINavigationController *yourNVC2 = [[UINavigationController alloc] initWithRootViewController:yourVC2];
UITabBarController *tabBC = [[UITabBarController alloc] init];
[tabBC setViewControllers:[NSArray arrayWithObjects:yourNVC1, yourNVC2, nil]];
self.window.rootViewController = tabBC;
and you should have a tab bar with navigation bar on all the views. Now to place the settings button in the navigation bar, add this to the viewDidLoad method in your view controller where you want to show the settings button -->
UIBarButtonItem *selectBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(selectorName:)];
self.navigationItem.rightBarButtonItem = selectBarButton;