I've created my toolBar and navigationBar and added the respective items to it. I'm still new to coding but its my understanding that UINavigationController only displays the navBar and toolBar for the viewController in the top of the seque. I'm doing this without storyboards and in swift.
If you want to add more than one bar button items to UINavigationBar programatically, you can do that like
UIBarButtonItem *btnShare = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(share)];
UIBarButtonItem *btnRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:btnShare, btnRefresh, nil]];
Hope this will help you.
Related
I am trying to add an UIBarButtonItem and an UIViewController with the following code:
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"back", #"")
style:UIBarButtonItemStylePlain
target:self
action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
But when I run my app, the UIBarButtonItem does not appear. Please Help.
Before you localize your string, make sure it works with a regular string. So then you know if the code for the button is incorrect or the if you didn't set up your localized string properly.
You need to embed your view controller into navigation controller at that time so can find the UIBarButtonItem. Because it is navigation item you need to embed inside the navigation controller.
When I layer SWRevealViewController over a normal UIViewController's view, to achieve a sliding left menu effect, it works as intended.
However, when this view controller is embeded on a UINavigationController, it overlays the navigation bar and hides the image and left and right BarButtonItems.
I'd appreciate some help finding a workaround.
This is how I'm adding this buttons:
UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage: [UIImage imageNamed:#"icon-back"]
style:UIBarButtonItemStylePlain
target:self.revealViewController action:#selector(revealToggle:)];
UIBarButtonItem *saveExitButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"out"]
style:UIBarButtonItemStylePlain
target:self action:#selector(saveExit)];
self.navigationController.navigationItem.leftBarButtonItem = revealButtonItem;
self.navigationController.navigationItem.rightBarButtonItem = saveExitButtonItem;
Added this repository to show my point I'm using this component https://github.com/John-Lluch/SWRevealViewController:
https://github.com/AresDev/revealtest.git
Main.storyboard is working, just change in project settings the main interface to NonWorking.storyboard to see the error.
I've found the solution, the navigation item that I have to add the buttom items is actually the RevealViewController navigation item.
UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage: [UIImage imageNamed:#"icon-back"]
style:UIBarButtonItemStylePlain
target:self.revealViewController action:#selector(revealToggle:)];
UIBarButtonItem *saveExitButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"out"]
style:UIBarButtonItemStylePlain
target:self action:#selector(saveExit)];
self.revealViewController.navigationItem.leftBarButtonItem = revealButtonItem;
self.revealViewController.navigationItem.rightBarButtonItem = saveExitButtonItem;
This what I understood from your question: You are trying to put together a navigation drawer view controller. If so then you could refer to: https://github.com/mutualmobile/MMDrawerController
If not then please elaborate your query.
I want to add a UIBarButton to the "navBar" of the viewController.
Here's my code:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:#"+ Contact"
style:UIBarButtonItemStylePlain
target:self
action:#selector(showPicker:)];
[self.navigationItem setLeftBarButtonItem:item animated:YES];
Nothing shows up on the "navBar". I also tried adding it through the storyboard but I couldn't connect it to the interface of my viewController as an IBAction.
Can't figure it out.
I'm using storyboard on xcode5.
and I want to display put right bar button on navigation within tabbar.
structure is tabbar controller -> navigation controller -> view controller
I tried following code on view controller.
But, Nothing is displayed.
- (void)viewDidLoad
{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"btn"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.rightBarButtonItem = addButton;
}
Does someone know what's wrong?
In your ViewDidLoad
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"More" style:UIBarButtonItemStyleBordered target:self action:#selector(toggleMenu:)];
or Storyboard
This is what I have on my storyboard:
And I wanna change both buttons images. I have a 13x46 image but I'm having problems using it as the button's images. I've searched around for some code but I wasn't very successful with them.
Any ideas?
This is pretty simple stuff, just alloc/initWithImage a couple of bar button items, then apply them.
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"myImage"] style:UIBarButtonItemStyleBordered target:self action:#selector(someMethod)];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"myImage"] style:UIBarButtonItemStyleBordered target:self action:#selector(someMethod)];
[[self navigationItem] setLeftBarButtonItem:leftItem];
[[self navigationItem] setRightBarButtonItem:rightItem];
This of course assumes that you are actually using a navigation controller. If you just have a navigation bar that you dragged and dropped in interface builder, then you need to make an IBOutlet for the navbar, link it up and then use something like this:
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Title"];
[item setRightBarButtonItem:rightItem];
[myNavBar pushNavigationItem:item animated:NO];
You may need to add a flexible space in between.