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.
Related
Actually I added two button (UIButtonBarItem) and assigned it to to NavigationItem's rightbarbuttonitem but only edit button is visible and add button is not visible but when we click on it it gets invoked ?
chk it out my code http://pastebin.com/Ew4zPEUz
Try using this code in ViewDidload method
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,60,30);
[button setImage:[UIImage imageNamed:#"iPhoneBackButton.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem=barButtonItem;
finally solved this problem .
just removed below line
self.navigationItem.rightBarButtonItem.tintColor=[UIColor clearColor]
and its works :)
So the code I have is as follows:
// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 23.5, 21.5)] autorelease];
// Create a custom button with the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Settings.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(-19, -3, 21.5, 21.5)];
[containingView addSubview:button];
// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
self.navigationItem.rightBarButtonItem = containingBarButton;
The issue is that my Settings.png original image size is 21.5, 21.5 and there fore the button is super hard to click. I have to tap really hard in order to trigger the UITouchUpInside to be triggered. This will clearly be rejected if I put it in the app store, as it is not in compliance with apple's HIG. Is there a way around this? I still need to use UIView as the container for the UIButton as I'd like to position it correctly on the UINavigationBar
Try to set a bigger fram to the button like 40,40 and set the content mode to be the center so the image will apear to be in the center.
button.contentMode = UIViewContentModeCenter;
Tell me if that helped
On a navigationBar as a rightBarButtonItem I take edit button by the following coding
self.navigationItem.rightBarButtonItem=self.editButtonItem;
but I want to change the color of edit button I mean I want to change the color of rightBarButtonItem. How can I do this?
plz reply as early as possible.
thanx in advance.
You will need to create a custom button and initialize your UIBarButtonItem using initWithCustomView.
Create your UIButton as you like if with your custom image and then:
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton];
You are constrained making standard UIBarButtonItem. But you can init it with any UIView of your choice, e.g. standard UIButton which you can color as you like.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 30, 20); // change as you like being constrained with navigation bar hieght
[myButton setTitle:#"My Button" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"Image for my button.png"] forState:UIControlStateNormal]; // <-- you can provide image instaed of title if you like
[myButton setBackgroundImage:[UIImage imageNamed:#"Background image for my button.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside]; // <-- don't forget configure this!!!
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
Visit this link. It is kind of same answer.
specially indicating or highlighting a toolbar button when a note is added in iphone
Usage of method [[UIBarButtonItem alloc] initWithCustomView:btnInfo]; can help.
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.
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;