What width should I set a UIBarButtonItem to make a square button? - iphone

I want to have a square button on my toolbar. My UIBarButtonItem uses an image which is wide enough to push my button into a rectangular shape. I looked through the docs, but couldn't find the best size to use.
Looking at other answers, 29.0 seems to be a common size, but I'd like to get confirmation. Here's how I set my button up:
UIBarButtonItem *locationButtonItem = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"location.png"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(locationButtonTapped:)];
[locationButtonItem setWidth:29.0f];
What width should I set my toolbar button to be to make it square?

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"location.png"] forState:UIControlStateNormal];
button.frame=CGRectMake(0,0, 29, 29);
[button addTarget:self action:#selector(locationButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithCustomView:button];
//[btnDone setTarget:self];
self.navigationItem.rightBarButtonItem = btnDone;
[btnDone release];
Try this ... maybe it will help you.

Just used UIKit Artwork Extractor and found out that the UIBarButtonImage is a perfect square when the picture inside is 13x16 #1x pixels. I simply measured the "plus" sign button used by UIBarButtonSystemItemAdd style (UINavigationBarAddButton.png) and also tried it by myself. Hope this will help someone.

Try this: [locationButton setWidth:locationButton.height];

You can't change the width of UIBarButton.
Instead of that, If you want custom width you will need to use initWithCustomView.
You can use the width property only if button in UIToolbar.

Related

How to remove space from UINavigationbar between Button

How to remove this space image here. Here is my code :
UIButton *postButton = [UIButton buttonWithType:UIButtonTypeCustom];
[postButton setImage:[UIImage imageNamed:#"post_btn.png"] forState:UIControlStateNormal];
postButton.frame = CGRectMake(0, 0, 44, 44);
[postButton addTarget:self.viewDeckController action:#selector(postAction:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:postButton];
thx for help.
As standard, you can't. And the cost of doing so is likely to be prohibitive as you would need to create your own custom navigation bar (or subclass) and manage the drawing yourself.
That said, the gap is there for a reason as touch detection isn't so accurate near the edge of the screen and fingers are big...
First there is no need to use UIButton but we can. you can use it UIBarButtonItem
UIImage *myImage = [UIImage imageNamed:#"post_btn.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:#selector(postAction:)];
self.navigationItem.rightBarButtonItem = button;
[button release];
As standard, you can't. but you can set manually.
1- setNavigationBarHidden hidden.
[self.navigationItem setNavigationBarHidden:TRUE];
2- Take one UIImageView size = 320 X 45. //as per your required height
3- set your button on it. make it height smaller then above image.

are customViews for UIBarButtonItems ignored for the leftBarButtonItem

From the documentation, I see this for the backBarButtonItem:
When this property is nil, the navigation item uses the value in its
title (page 10) property to create an appropriate back button. If you
want to specify a custom image or title for the back button, you can
assign a custom bar button item (with your custom title or image) to
this property instead. When configuring your bar button item, do not
assign a custom view to it; the navigation item ignores custom views
in the back bar button anyway.
I didn't know if this was the same for leftBarButtonItem? Basically I have this code:
UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIImage *homeImage = [UIImage imageNamed:#"icon_house.png"];
[homeButton setImage:homeImage forState:UIControlStateNormal];
[homeButton addTarget:self action:#selector(homePressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *homeBBI = [[UIBarButtonItem alloc] initWithCustomView:homeButton];
Prior to iOS 5, I put this in a UIToolBar and it showed up fine. Now I want to put this as the leftBarButtonItem, to the right of the UINavigationController's backButton. When I set it, it does not show up at all. There is no image. However, when I create some button like this:
UIBarButtonItem *hButton = [[UIBarButtonItem alloc] initWithTitle:#"home" style:UIBarButtonItemStylePlain target:self action:#selector(homePressed:)];
and set it as the leftBarButtonItem, it shows up. I didn't know how I could get my custom icon for my home button without the borders. When I use:
UIBarButtonItem *hButton2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"icon_house.png"] style:UIBarButtonItemStylePlain target:self action:#selector(homePressed:)];
I get a border around my house_icon that i do not want. Thanks.
try this
UIButton *TastoVersamento = [UIButton buttonWithType:UIButtonTypeCustom];
[TastoVersamento setImage:[UIImage imageNamed:#"Versamento.png"] forState:UIControlStateNormal];
[TastoVersamento addTarget:self action:#selector(Click_Versamento:) forControlEvents:UIControlEventTouchUpInside];
[TastoVersamento setFrame:CGRectMake(0, 0, 40, 40)];
[[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:TastoVersamento] autorelease]];
backBarButtonItem and leftBarButtonItem are different. backBarButtonItem is the default if no leftBarButtonItem is given. It will also show up even if leftBarButtonItem is set if leftItemsSupplementBackButton is YES.
In your first example, you could try [homeButton sizeToFit]. I think the frame may not be getting set correctly.
But as to your last comment, I think you won't be succesful in removing the border from navigation item buttons. I'm pretty sure they're hard coded. In fact, if you add a button that has a border, you'll get two, one from the button and one from the navigation item.

Allow a UIBarButtonItem to touch top & bottom of UINavigationBar

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...

Is there an easy way to change the text color of a UIBarButtonItem without using an image?

Is there an easy way to change the text color of a UIBarButtonItem without using an UIImage as the background?
By default the text is always white. I'd like to make it black.
Thank to IOS 5.0 you do not need to use images for that.You can set various text attributes with following code.
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"Title" style:UIBarButtonItemStyleBordered target:nil action:nil];
if ([button respondsToSelector:#selector(setTitleTextAttributes:forState:)]) {
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
kTextColor,UITextAttributeTextColor,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:textAttributes
forState:UIControlStateNormal];
}
I'd rather use a UIButton inside a UIBarButtonItem and customize that one.
This is an example with custom graphics for a custom UIButton. The idea stays the same use initWithCustomView of the UIBarButtonItem to put something else in it which is easily customizable.
self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton setImage:[UIImage imageNamed:#"webview_close_button_normal.png"] forState:UIControlStateNormal];
[closeButton setImage:[UIImage imageNamed:#"webview_close_button_pressed.png"] forState:UIControlStateHighlighted];
closeButton.frame = CGRectMake(0, 0, 121, 36);
[closeButton addTarget:self action:#selector(closeAdViewController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * aBarButtonAdClose = [[[UIBarButtonItem alloc] initWithCustomView:closeButton] autorelease];
You can set a custom view by using
[[UIBarButtonItem alloc] - (id)initWithCustomView:(UIView *)customView]
The direct answer to your question is no.
You can't simply change the color of a UIBarButtonItem by setting one of its properties. You can set the tintColor property of the UIToolBar or UINavigationBar that contains the UIBarButtonItems, but that also affects the color of the toolbar itself and doesn't offer too much customization.
If that doesn't work for you, a custom view as the other answers suggest is a fine idea.
Good luck!

How to remove CustomView from UIBarButtonItem?

I'm wanting to remove a image from a uibarbuttonitem and take it back to the default button style. The code I'm using to set the customview for the baritem is:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 79, 29.0);
[backButton setImage:[UIImage imageNamed:#"imagehere.png"] forState:UIControlStateNormal];
[self.myItem initWithCustomView:backButton];
So what I'm asking is how can I remove the backButton CustomView and take it back to my default style? Thanks.
First off, [self.myItem initWithCustomView:backButton] is wrong. You should always be doing something like self.myItem = [[UIBarButtonItem alloc] initWithCustomView:backButton].
It does not seem that you can change the type of the UIBarButtonItem; when I tried assigning nil to the customView property in a test app just now it screwed up the whole toolbar. Your best bet would be to just create a new UIBarButtonItem and reset the toolbars items array.
Simply remove it by
self.myItem = nil;