A designer asked me to add pagination to a UINavigation bar in a project with its target building for ios4.3+.
I know you can add two buttons to the right side of a Navigation bar as explained here in this blog : enter link description here
But to get the appearance requested to by the designer
Do I need to add images to my button or is this some kind of native control(Special UISegmentedControl?) I haven't seen before? Or Just 2 custom buttons with background images.
Thanks,
-Code
I implemented the same function with a segmented control and without defining custom buttons with images.Instead of adding images for arrows i used the the corresponding Unicode chars and instantiated the segmented control with them.
UISegmentedControl *toggleSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"\u25B2",#"\u25BC",nil]];
UIBarButtonItem *toggleSegmentedControlBarItem = [[UIBarButtonItem alloc] initWithCustomView:toggleSegmentedControl];
self.navigationItem.leftBarButtonItem = toggleSegmentedControlBarItem;
Then your leftBarButtonItem is replaced by the segmented control.
If I undestand, you want put a image in each UIBarButtonItem and setting button status (when is clicked or normal condition). If yes, you can try this code
`UIImage *buttonImageNormal = [UIImage imageNamed:#"image.png"];
UIImage *buttonImagePressed = [UIImage imageNamed:#"imagepressed.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImage forState: UIControlStateHighlighted];
UIBarButtonItem aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
// Create and Set the Target and Action for aButton
[myButton addTarget:self action:#selector(myButtonClicked:) forControlEvents:UIControlEventTouchUpInside];`
Related
I have a UIToolbar which I am trying to put some custom UIBarButtonItems on. However, when I use the code below, the button shows up with NO border.
UIImage *cameraRollButtonImage = [UIImage imageNamed:#"Flash.png"];
UIButton *cameraRollButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cameraRollButton setImage:cameraRollButtonImage forState:UIControlStateNormal];
cameraRollButton.frame = CGRectMake(0.0, 0.0, cameraRollButtonImage.size.width, cameraRollButtonImage.size.height);
// Initialize the UIBarButtonItem
cameraRollButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cameraRollButton];
[cameraRollButtonItem setStyle:UIBarButtonItemStyleBordered];
//Add the Buttons to the toolbar
NSArray *toolbarItems = [NSArray arrayWithObject:cameraRollButtonItem];
[self.cameraTabBar setItems:toolbarItems];
This displays the button just fine, however, there is NO button border (like standard the UIBarButtonItem). So the line [cameraRollButtonItem setStyle:UIBarButtonItemStyleBordered]; doesn't seem to do anything.
Does anyone have any experience with this?
I would like to be able to eventually rotate the image in the button when the device orientation is changed (keeping the toolbar static), so simply adding an image to the UIBarButtonItem doesn't work; I need to get this to work with by using the customView property.
Many thanks!
Brett
Have you considered creating your own button image with a border? You can use it as the backgroundImage of a UIButton:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:_backgroundImage_ forState:UIControlStateNormal];
// So that the button does not gray out when disabled
[button setBackgroundImage:_backgroundImage_ forState:UIControlStateDisabled];
[button setImage:_cameraImage_ forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 125, 30);
You could then use button with initWithCustomView:.
The PSD file here might give you an overview of how to create your own button.
I want to set an image to the back button of UINavigationController
Use this code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,36,30);
[button setBackgroundImage:[UIImage imageNamed:#"backgroundImage.png"] forState:UIControlStateNormal];
[button addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
Or use this library. Very easy and good looking result.
https://github.com/boctor/idev-recipes/tree/master/CustomBackButton
You cannot change the default back button since it will not let you override it. Instead you have to use the leftBarButtonItem to create a custom "back" button.
More information here: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006934-CH3-SW25
Some prior code to reference: UINavigation controller button like backButton
Just call popViewControllerAnimated on the navigation controller and it will go back.
I have the following in my view did load on my table view controller:
UIButton *change_view = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[change_view setTitle:#"List" forState:UIControlStateNormal];
[change_view addTarget:self action:#selector(toggleView:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView: change_view];
self.navigationItem.rightBarButtonItem = button;
When I run the program, it doesn't show any title or the rounded rectangle button. However, if I change the type to UIButtonTypeInfoLight, it will show up... so what is the problem?
I'd prefer the way below to set the button's bounds.
[button setTitle:#"Title" forState:UIControlStateNormal];
[button sizeToFit];
Try to set an appropriate frame to your change_view.
When you use UIButtonTypeInfoLight type button uses some built-in size (likely depended on icon used for it), with UIButtonTypeRoundedRect type default frame is applied which must be CGRectZero rect.
I use custom image for my navigation bar buttons using the following code, that allows me to make custom add button. I want to be able to do the same for the edit button item.
UIImage *image=[UIImage imageNamed:#"Plus.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(add) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = barButtonItem;
[barButtonItem release];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
I'm doing this because I want to change the button text color.I would appreciate your help,
Sarah
You can customize the edit button in the same way you can customize any other navigation control button, for example...
self.editButtonItem.style = UIBarButtonItemStyleBordered;
self.editButtonItem.title = #"Custom";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
Unfortunately changing the text colour is not as easy as setting a 'color' property, since that property doesn't exist (again this applies to all navigation control buttons). It is however possible using a label as described in the following post...
iPhone Navigation Bar Title text color
In the Maps app, when you press the tracking button in the lower left hand corner, it glows showing that it's pressed. This makes it behave like a radio button, and it will un-glow once you move the map. Is there a simple way to but a button into the pressed state?
I believe you are looking for the showsTouchWhenHighlighted property for the UIButton class. Give this a try.
myButton.showsTouchWhenHighlighted = YES;
Set its style to UIBarButtonItemStyleDone.
You could make its customview a custom button like this:
`
UIImage *image = [UIImage imageNamed:#"someimage"];
UIImage *imageHL = [UIImage imageNamed:#"someimage_selected"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
[button setImage:imageHL forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
myBarButtonItem.customView = button;
`
Use UIBarButtonItemStylePlain.
UIBarButtonItemStylePlain
Glows when tapped.
If you have a UIBarButtonItem with image/text or UIBarButtonSystemItem, then use UIBarButtonItemStylePlain
barButton.style = UIBarButtonItemStylePlain;
If you have a UIBarButtonItem with a custom view, then, for each UIButton should add the following code:
uiButton.showsTouchWhenHighlighted = YES;