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.
Related
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.
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 have 4 buttons on the top bar setup using array of buttons with leftBarButtonItems and rightBarButtonItems. When I push to another view and return the buttons are gone. The area where the buttons should be located are not active (cannot select). If I put the application in the background and bring it back to the foreground the buttons are restored.
Any suggestions?
Thanks
Here is the code that initializes the buttons:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *selectButton = [[UIBarButtonItem alloc] initWithTitle:#"Select" style:UIBarButtonItemStylePlain target:self action:#selector(selectPlayers:)];
UIBarButtonItem *manageButton = [[UIBarButtonItem alloc] initWithTitle:#"Manage" style:UIBarButtonItemStylePlain target:self action:#selector(managePlayers:)];
self.navigationItem.rightBarButtonItems =
[NSArray arrayWithObjects:manageButton, selectButton, nil];
UIBarButtonItem *TeamsButton = [[UIBarButtonItem alloc] initWithTitle:#"Teams" style:UIBarButtonItemStylePlain target:self action:#selector(CreateTeams:)];
UIBarButtonItem *TransferButton = [[UIBarButtonItem alloc] initWithTitle:#"Transfer" style:UIBarButtonItemStylePlain target:self action:#selector(Transfer:)];
self.navigationItem.leftBarButtonItems =
[NSArray arrayWithObjects:TeamsButton,TransferButton, nil];
}
I found a workaround. The problem view has the status bar hidden. The pushed view shows the status bar. If I also hide the status bar on the pushed view the buttons no longer disappear. This looks like a bug in xcode or IOS.
I am trying to add a UIBarButtonItem with the style of UIBarButtonSystemItemCompose. According to the apple docs it should display a compose icon that consists of a square outline. When I use the following code it just displays a red button. Does the icon only work if the uibarbuttonitem if placed inside of a UIToolBar and not a navigation bar.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:nil
style:UIBarButtonSystemItemCompose
target:self
action:#selector(tweetPressed:)] autorelease];
You are creating the button incorrectly. You need to use the proper init... method.
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(tweetPressed:)];
self.navigationItem.rightBarButtonItem = btn;
[btn release];
Look at the docs for the init... method you used. Look at what type should be passed for the style parameter and look at what the valid values are.
I'm trying to add a custom button (shape and color) to my UIToolBar, but it comes out much differently than it should.
What button looks like:
What it looks like in the bar:
Here's the code I used to add it:
UIImage *backButtonImage = [UIImage imageNamed:#"back-button.png"];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:#selector(backButtonTapped)];
[toolBarItems addObject:backButton];
What exactly am I doing wrong here?
Try using initWithCustomView: instead of the other initialization method
So you should first create a UIButton with the custom image and selector you want and then use this piece of code
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:yourButton];
[toolBarItems addObject:backButton];