I would like to setup the navigation controller bar button item to be an image.
I have read the following - Add image to a navigationItem's title
I understand how to setup this as an image. I am actually looking to set this up as the setting cog, within the bar button item (like a cog icon). I have seen this before in other apps, I would like to know if there is a default style to initialise for this or normally if this is achieved by the image route as above.
If this is above, does anyone know where this (kind of) default image styled icon is available, or would I need to make one?
Have you tried using the initWithCustomView option of UIBarButtonItem? A sample code will look like the below line.
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"settings.png"]]];
Another way to do this is,
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"settings.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(buttonTapped)];
If you use storyboard, you can directly:
Drag a Button in the navigation bar instead of a Bar Button Item. It will automatically create a Button Bar Item for you with a Button inside it.
Enter a title for the Button Bar Item
Set an image for the Button with a Custom type.
It should works.
Related
I've added a UISegmentedControl to a UINavigation Bar, and when I tried to add a UIBarButtonItem programmatically in viewDidLoad viewController using the following code:
UIBarButtonItem *timeTableBarButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"blueRoundRect"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(popUpTimeTable)];
self.navigationItem.rightBarButtonItem = timeTableBarButton;
There are no button showing at all. I wonder since I already have a UISegmentedControl, is it possible to do so?
It is possible to add the Bar button in navigation bar and there is no way your segment control should be blocking right bar button item. Please make sure that your self.navigationItem is not nil.
I used third party code (menu) in my application to change my navigation controller back button title. Therefore I used code like this:
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Geri" style :UIBarButtonItemStyleBordered target:self action:#selector(myCustomBack)] autorelease];
This code work fine but there is no default left arrow image in iOS 7, also rectangle button created in iOS 6. How can I add default left image (not custom) in back button?
You need own arrow image.
Create regular UIButton with custom style and background image (arrow) also set action, then create UIBarButtonItem and put this button as custom view;
Is it possible to have a UIBarButtonItem that is disabled (non-interactive) as well as highlighted. My desire is to have a status icon in the NavBar, so I want it to be highlighted (not gray), but at the same time I don't want it to be a button. Can you add things besides UIBarButtonItems to a NavBar?
Thanks!
Sure you can!
Besides the name, UIBarButtonItem is not necessarily a button. You can add an image or anything else you want.
There's a initWithCustomView constructor where you can pass any view as parameter. For example:
UIImageView *statusImageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"status"]];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView: statusImageView];
If you want a clickable status, you can put the ImageView inside the UIButton.
I am currently working with a UISplitViewController and instead of having this default slide-out-menu, i want a real UIPopover that appears if i click the UIBarButtonItem. What do i have to do and is there an easy way of configuring this ?
You first need to overwrite the left bar button item, with the button that can be used to display the popover.
Use the following -
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithTitle:#"Popover" style:UIBarButtonItemStylePlain target:self action:#selector(presentPopover:)];
self.navigationItem.hidesBackButton = NO;
self.navigationItem.leftBarButtonItem = nil;
Now you can use the target added, and then perform the associated function, as per your choice.
-(IBAction)presentPopover:(id)sender
{
// Perform your operations
}
Then just use a popover view controller instead with the same content you would have used in the splitview pane that slides out.
I want to change the background image of my UIBarButtonItems. In the root view, I want them to be a certain background image (but still rounded-rect buttons) in the nav bar, but in the next view, I have a UIToolBar where I want it to have different backgrounds yet again.
I used [[UIBarButtonItem] appearance] in my app delegate to change all of them, but I now realize for some I want it one style and for others yet another.
More importantly, I want to change not only the background image, but the shape for some. For one of the UIToolBar's UIBarButtonItems I want it to be in the shape of a back button in the navigation bar. How would I achieve this look?
Can I achieve both of these with the method outlined here?
Basically: How do I make custom UIBarButtonItems all over the app, and have some with different shapes?
If you want the buttons to look different in different view controllers, you should set them within the view controller, not in the delegate. Add code in either the init method or the viewWillAppear: method to customise the buttons. If you are customising UIToolbar items then use code similar to the link you gave. If it's for the navigation bar, do something like this:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//Customise button with background etc that you want
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = item;
//Release item if you're not using ARC
You can use the 'appearanceWhenContainedIn' method from the UIAppearance Protocol http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html
So for the Toolbar, you'd use:
[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil]
And for the NavBar:
[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]