UInavigatonbar items - ios

I add a uinavigationcontroller as a subview on a uiviewcontroller (rootviewcontroller of aap) and this view controller is a subview as a rootviewcontroller on self.window.
in Appdelegate I use:
self.window.rootViewController = self.rootVC;
and in viewdidload of my RootViewController I wrote:
[self.navVC.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
[self.navVC.navigationController.navigationBar setTranslucent:YES];
self.navVC.navigationController.navigationBarHidden = NO;
[app.rootVC.view addSubview:self.navVC.view];
self.navVC.delegate = self;
Here, navigationbar is displaying but I am unable to display uibarbutton items on navigation bar. Example code:
leftBtn = [[UIBarButtonItem alloc] initWithTitle:#"Left" style:UIBar
flxBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBa
rightBtn1 = [[UIBarButtonItem alloc] initWithTitle:#"Right" style:UIBar
rightBtn = [[UIBarButtonItem alloc] initWithTitle:#"Right" style:UIBar
[self.navVC.navigationItem setLeftBarButtonItem:leftBtn];
[self.navVC.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
Can somebody help me out to resolve my problem. How can I change appearance of navigationbar and how the uibarbutton items can be displayed ?

You will simply need to change your
[self.navVC.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
to
[self.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
and
[self.navVC.navigationItem setLeftBarButtonItem:leftBtn];
to
[self.navigationItem setLeftBarButtonItem:leftBtn];`

Related

Navigation Bar default view

In my app I have a tableViewController where I have added a UINavigationController and a BackButton programmatically:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
[self.view addSubview:navController.view];
self.title =#"A";
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"back" style:UIBarButtonItemStyleBordered target:self action:#selector(backPressed:)];
self.navigationItem.leftBarButtonItem = btw;
But the output is not the same as the default navigation bar and as the default back button.
This is what I mean with default navigation bar and default back button:
And this is how it looks with my custom code:
How could I get the same output with code?
Try to set up navigationBar frame with following code:
self.navigationController.navigationBar = CGRectMake(self.navigationController.navigationBar.frame.origin.x, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, 64);
and use this method:
-(UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
Don't forget to set up delegate of navigation bar
self.navigationController.navigationBar.delegate = self;

Add a navigationbar with a back button to the app [duplicate]

This question already has answers here:
how to create back button in navigation bar
(3 answers)
Closed 9 years ago.
I need a step by step tutorial to add a navigation bar including a back button to my project.
My rootViewController defined in the AppDelegate is the LoginViewController. After successfull login it goes to the MainView and then to the SingleView
How would I add a navigation bar and a back button? This is the last thing I need for my app. I already tried many things
for example:
everything in the viewDidLoad method
First Try
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
Second Try
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
What exactly do I write into the RootViewController and what do I write into the other UIViewController to get a butotn?
Edit 2 after Popeye's suggestion
//Appdelegate.m
LoginViewController *viewController = [[LoginViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:viewController];
[navCon setNavigationBarHidden:NO];
self.window.rootViewController = viewController;
//LoginViewController.m
[self.navigationController setNavigationBarHidden:NO];
ToDoListViewController *viewController = [[ToDoListViewController alloc] init];
viewController.stringUserId = //userid//;
[self presentViewController:viewController animated:NO completion:nil];
//ToDoListViewController.m
[self.navigationController setNavigationBarHidden:NO];
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = #"Back";
UINavigationItem *right = [[UINavigationItem alloc] initWithTitle:#"Hello!"];
right.leftBarButtonItem = myBarButtonItem;
[self.navigationController.navigationBar pushNavigationItem:right animated:YES];
still, no buttons!
Best way is add NavigationController to your rootViewController and you also able to hide and show NavigationBar by using following code
yourNavigationController.navigationBarHidden:YES/NO;
you can add rootViewController with navigationController by
LoginViewController *loginVC = [[LoginViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginVC];
navCon.navigationBarHidden = YES/NO;
self.window.rootViewController = navCon;
.
.
.

UINavigation bar back button not responding

I have a cameraview from which I'm pushing another view of with TableViewController. I can push the second view from my main view by using navbar controller. But the back button from the second view which is table view does not respond. I can't figure out why.
Thanks in advance.
#WrightCS Thank you for your immediate response.
In my app delegate.
ViewController *vc= [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController :navController];
[self.window makeKeyAndVisible];
return YES;
Right now I'm using a temporary button to push the view.
-(IBAction)testButtonPressed {
TableView *tableVC = [[TableView alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:tableVC animated:YES];
}
There are a few reasons your back button may not work:
You are using a custom UIButton and the frame/bounds are not correct you can use [yourButtonName sizeToFit]
to size it properly.
You are adding a button programmatically and not adding a click event:
[yourButtonName addTarget:self action:#selector(yourButtonNameTapped:) forControlEvents: UIControlEventTouchUpInside];
Don't forget your event.
...
-(void)yourButtonNamedTapped:(id)sender
{
...
}
docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html
If you are using a UINavigationController, back will automatically show up when you use [self.navigationController pushViewController:viewControllerName];
-(IBAction)testButtonPressed {
TableView *tableVC = [[TableView alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[self.navigationController pushViewController:tableVC animated:YES];
}
I think it will be helpful to you.
- (void)viewDidLoad
{
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonDidClicked:)];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
}
-(void)backButtonDidClicked :(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
It should work for you!!

Navigation Controller Toolbar Buttons issue - Xcode

I'm trying to add some button to the toolbar of my navigationController: i see the toolbar, but without the buttons. This is the portion of my code where I set the toolbar...
(this is my AppDelegate)
// Create a table view controller
RootViewController *rootViewController = [[RootViewController alloc]
initWithStyle:UITableViewStyleGrouped];
rootViewController.managedObjectContext = context;
rootViewController.entityName = #"County";
//Navigation Controller
UINavigationController *aNavigationController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
//Barbuttons
UIBarButtonItem *homeButton;
homeButton = [[[UIBarButtonItem alloc] initWithTitle:#" Inizio " style:UIBarButtonItemStyleBordered target:self action:#selector(home)] autorelease];
UIBarButtonItem *barButton;
barButton = [[[UIBarButtonItem alloc] initWithTitle:#" Funzioni online " style:UIBarButtonItemStyleBordered target:self action:#selector(caricamappa)] autorelease];
UIBarButtonItem *creditsButton;
creditsButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"credits2.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(credits)] autorelease];
NSArray *baritems = [NSArray arrayWithObjects: homeButton, barButton, creditsButton, nil];
[window addSubview:[navigationController view]];
[self.navigationController.toolbar setItems:baritems];
[self.navigationController setToolbarHidden:NO];
[window makeKeyAndVisible];
[rootViewController release];
[aNavigationController release];
Any idea about my mistake?
You should add buttons to navigationItem property of your rootViewController, not to the toolbar of navigation controller.
Something like:
rootViewController.navigationItem.rightBarButtonItems = barItems;
Check out the documentation, especially this part:
Management of this toolbar’s contents is done through the custom view
controllers associated with this navigation controller. For each view
controller on the navigation stack, you can assign a custom set of
toolbar items using the setToolbarItems:animated: method of
UIViewController.

How to add a right bar button to a navigation bar in iphone

I want to add a right bar button item to the navigation bar, so that on click, it performs certain a function.
I have created the following code to add the right bar button item, but after it is done, the bar button item is not getting displayed in navigation bar:
-(void)viewDidload{
self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(Add:)] autorelease];
}
-(IBAction)Add:(id)sender
{
TAddNewJourney *j=[[TAddNewJourney alloc]init];
[app.navigationController pushViewController:j animated:YES];
[j release];
}
Let assume, you have a UIViewController, like this:
UIViewController *vc = [UIViewController new];
And you added it to navigation controller:
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
In this way rightBarButtonItem will NOT getting displayed:
nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"xyz" style:UIBarButtonItemStyleDone target:self action:#selector(xyz)];
BUT this way IT WILL appear:
vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"xyz" style:UIBarButtonItemStyleDone target:self action:#selector(xyz)];
Use your own viewcontroller instead of the navigationcontroller to refer navigationItem.
-(void)viewDidLoad
{
[super viewDidLoad];
app.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(Add:)] autorelease];
}
-(IBAction)Add:(id)sender
{
TAddNewJourney *j=[[TAddNewJourney alloc]init];
[app.navigationController pushViewController:j animated:YES];
[j release];
}
Try the other answers. I posted this answer so that it will work if your viewcontroller does not have a navigation controller which i think is the problem.
Add this code in viewdidload
UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:#"Calculate" style:UIBarButtonItemStylePlain target:self action:#selector(nextview:)];
self.navigationItem.rightBarButtonItem = chkmanuaaly;
Most of the answers here address setting the UINavigationBar's rightBarButtonItem from within the new VC after it is shown. My need, which was actually answered by János, is to set the UINavigationItem items from the CALLING UIViewController. Hence, the following code (Thanks, János).
// from within self (aka the calling controller):
UIViewController *c = [[[UIViewController alloc] init...] autorelease];
c.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissModalViewControllerAnimated:)] autorelease];
c.navigationItem.title = #"Title Goes Here";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:c] autorelease];
nav.navigationBarHidden = FALSE;
[self presentModalViewController:nav animated:YES];
Now, granted, this may seem redundant to János' answer, but I need more rep points in order o mark his response up. ;-)
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addUser)];
self.navigationItem.rightBarButtonItem=add;
[add release];
I guess it will be more complete response and it should help in any situation:
-(void)viewDidLoad{
//Set Navigation Bar
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
//Set title if needed
UINavigationItem * navTitle = [[UINavigationItem alloc] init];
navTitle.title = #"Title";
//Here you create info button and customize it
UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
//Add selector to info button
[tempButton addTarget:self action:#selector(infoButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];
//In this case your button will be on the right side
navTitle.rightBarButtonItem = infoButton;
//Add NavigationBar on main view
navBar.items = #[navTitle];
[self.view addSubview:navBar];
}
Use the following code:
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addUser)];
self.navigationItem.rightBarButtonItem=add;
[add release];
Hope this helps you out.
Enjoy!
Swift:
let rightButton = UIBarButtonItem(image: UIImage(named: "imageName"),
style: .plain,
target: self,
action: #selector(buttonTapped))
navigationItem.setRightBarButton(rightButton, animated: false)
#objc func buttonTapped(sender: UIBarButtonItem!) {
// Implement action
}
UIButton *dButton=[UIButton buttonWithType:0];
dButton.frame=CGRectMake(50,50,50,50);
[dButton addTarget:self action:#selector(clickdButton:)
forControlEvents:UIControlEventTouchUpInside];
[dButton setImage:[UIImage imageNamed:#"iconnavbar.png"]
forState:UIControlStateNormal];
dButton.adjustsImageWhenHighlighted=NO;
dButton.adjustsImageWhenDisabled=NO;
dButton.tag=0;
dButton.backgroundColor=[UIColor clearColor];
UIBarButtonItem *RightButton=[[[UIBarButtonItem alloc] initWithCustomView:dButton]autorelease];
self.navigationItem.rightBarButtonItem=RightButton;
and then:
-(IBAction)clickdButton:(id)sender{
classexample *tempController=[[classexample alloc] init];
[self.navigationController pushViewController:tempController animated:YES];
[tempController autorelease];
}

Resources