It is looks like a very simple question But i do not know where the mistake is.
I have an navigation bar i am placing a custom nav bar back button
The action is performed through out the "pink" color as shown in the image below
My code is below:
//-------back button start
UIImage *myImage1 = [UIImage imageNamed:#"Back.png"];
UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton1 setImage:myImage1 forState:UIControlStateNormal];
myButton1.showsTouchWhenHighlighted = YES;
myButton1.frame = CGRectMake(0.0, 3.0, 40,30);
[myButton1 addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1];
self.navigationItem.leftBarButtonItem = leftButton;
//-------back button end
I tried even this way also like "myButton1.frame = CGRectMake(0.0, 3.0, 20,30);"
Even i tried by changing the button width and height also. Then it is decreasing the width of the back button only .
Thanks in advance
You can set your button size as image size.
myButton1.frame = CGRectMake(0.0, 0.0, 30.0,30.0);
And please check that you don't have any shadow and transparency around image.
Related
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
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.
Take a look at the Droplr iPhone app:
Notice how the UIBarButtonItems are able to touch the right, left, top, and bottom of the screen/navigation bar?
How can I achieve something similar? Here's how I make a sample UIBarButton and set it to the right item:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *bb = [[[UIBarButtonItem alloc] initWithCustomView:button]autorelease];
[self.navigationItem setRightBarButtonItem:bb animated:YES];
However, it is not right aligned, and has quite a bit of margin from the top & the bottom. My image size is correct (44px), and it looks like it shrinks it to fit a frame of sorts.
So, how can I do this?
Edit: Whoops, the top/bottom spacing was my fault. However, I can't figure out how to align the bar button flush with the left/right side. Here's what I mean: (sorry for the ugly button, it was just a test)
I tried setting the image inserts, but it didn't seem to do anything.
UIView *rightview = [[UIView alloc] initWithFrame:CGRectMake(0,0,30,30)];
UIButton *searchbutton = [[UIButton alloc] initWithFrame:CGRectMake(2,2,50, 30)];
[searchbutton setImage:[UIImage imageNamed:#"some-pic.png"] forState: UIControlStateNormal];
[rightview addSubview:searchbutton];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:rightview];
self.navigationItem.rightBarButtonItem = customItem;
[customItem release];
I use a customView for the rightBarButtonItem and I get it right aligned.
Just try a bit with the CGRectMake-Numbers for the x-coordinate, for testing I added to high numbers...
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
I have a navigation controller which also has a table view. I want to add an image left side of the navigation bar on top programmatically. When i tried to add by the below code in viewDidLoad:
CGRect frame = CGRectMake(0.0, 0.0, 94.0, 33.0);
UIImageView *image = [ [UIImageView alloc]initWithImage:[UIImage imageNamed:#"topBarImage.png"] ];
image.frame = frame;
[self.view addSubview:image];
but it is adding to top of the table view instead of navigation tab. I think, this image should be added as navigationItem. But i don't how to add an image on left side of navigation bar programmatically. Could some one guide please?
Clave/
you just use a custom view for a bar button item. Here is an example of one with a custom image button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(blah) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
You want to do it this way because the title of the nav controller will be auto resized this way. If you do it your way (subview of the nav bar) the text will go behind or ontop of the image
edit: it will also stay on the screen as you pop and push view controllers.
edit2: once again, you can use an imageview as the custom view and not the button. Here is code
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setImage:[UIImage imageNamed:#"myImage.png"]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:iv];
[iv release];