Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-12-2009, 12:20 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 5
60day is on a distinguished road
Default Color image on a UIBarButtonItem on a UIToolBar?

Is there anyway to put a color image on a UIBarButtonItem that is on a UIToolBar? Normally if you put a color image it just creates a white mask.

My end goal is to have a button on a UIToolBar with a color image on it. Would love to know if there is a way to do it with a UIBarButtonItem, some other type of button, or a work around.

Thanks.
60day is offline   Reply With Quote
Old 11-12-2009, 12:22 AM   #2 (permalink)
girls? any new species ?
 
sindhutiwari's Avatar
 
Join Date: Nov 2008
Location: INDIA
Posts: 547
sindhutiwari is an unknown quantity at this point
Send a message via MSN to sindhutiwari Send a message via Yahoo to sindhutiwari Send a message via Skype™ to sindhutiwari
Default

put the tool bar .. take a normal button not the tool bar button .. assign the image .. place it on tool bar ..

its the worst solution ..

experts will help u out in this ..

Quote:
Originally Posted by 60day View Post
Is there anyway to put a color image on a UIBarButtonItem that is on a UIToolBar? Normally if you put a color image it just creates a white mask.

My end goal is to have a button on a UIToolBar with a color image on it. Would love to know if there is a way to do it with a UIBarButtonItem, some other type of button, or a work around.

Thanks.
__________________
In order to succeed, your desire for success has to be greater than your fear for failure
sindhutiwari is offline   Reply With Quote
Old 11-12-2009, 07:59 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: France
Posts: 1
Green Snail is on a distinguished road
Default

Quote:
Originally Posted by sindhutiwari View Post
put the tool bar .. take a normal button not the tool bar button .. assign the image .. place it on tool bar ..

its the worst solution ..

experts will help u out in this ..

I used the same ugly workaround... It works but I will be very interrested for a "real" & clean solution.
Green Snail is offline   Reply With Quote
Old 11-12-2009, 08:34 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

Quote:
Originally Posted by sindhutiwari View Post
put the tool bar .. take a normal button not the tool bar button .. assign the image .. place it on tool bar ..

its the worst solution ..

experts will help u out in this ..
Unfortunately, there isn't a really good way around this problem. One minor improvement/clarification to the above is that instead of adding the UIButton directly as a subview to the toolbar, you could create a UIBarButtonItem using initWithCustomView:, with the UIButton as the custom view. This way, you don't have to try and manually position the button around the existing toolbar items. You'll still need to make your button image mimic the look and feel of the toolbar buttons, though, which is annoying.

One other point about this approach: if you do this, be sure to set the frame of the UIButton to the size of the image. Otherwise, you can run into the weird issue of being able to see the button, but not being able to tap on it, because the bounds of the button is actually initialized to (0, 0, 0, 0). I've been burned by this before.
ChrisL is offline   Reply With Quote
Old 11-12-2009, 10:59 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 5
60day is on a distinguished road
Default

Thanks guys. I was hoping there was a clean way to do this but it doesn't sound like it.

ChrisL, I'm a little unfamiliar with custom views and setting the frame of the button to the size of the image. Understand what you're saying though. It seems to make sense. Any chance you could post sample code on how you did this? Avoiding having to manually position the button would be a big, big help.

Think I'm going to submit this as a feature request to Apple.
60day is offline   Reply With Quote
Old 11-12-2009, 10:06 PM   #6 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

Quote:
Originally Posted by 60day View Post
ChrisL, I'm a little unfamiliar with custom views and setting the frame of the button to the size of the image. Understand what you're saying though. It seems to make sense. Any chance you could post sample code on how you did this? Avoiding having to manually position the button would be a big, big help.
Sure, no problem. UIBarButtonItem is a weird class it that it can behave in different roles depending on what properties you set. It's kind of poorly named in that while it usually behaves like a button, it can also be other things, like fixed space, etc. It can also act as a container for an arbitrary UIView, which is what we want to do here; it just happens that our custom UIView is actually a UIButton.

The code would go something like this:
Code:
//load the image
UIImage *buttonImage = [UIImage imageNamed:@"someImage.png"];
	
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
	
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
	
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Here, I'm loading the image and creating the button programmatically (in, for example, the loadView method of the view controller), but it wouldn't make much difference if you did this part in interface builder. The important line is
Code:
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
which creates a bar button item that acts as a container for a custom view; in this case, the custom view is button. (I'm not sure whether its possible to create a bar button item with a custom view in interface builder; I've never seen this option, but then I've never really looked for it).

Once this is done, you'd add it to the toolbar items array, just as you would any other bar button item. If you've never done this in code, it would go something like this
Code:
//Create an array to hold the list of bar button items
NSMutableArray *items = [[NSMutableArray alloc] init];

//add the items, including our custom button
...
[items addObject:customBarItem];
...
//set the items on the current toolbar
toolbar.items = items;
[items release];
(Here, I'm assuming you have a UIToolbar called toolbar; if your toolbar is the standard toolbar that's part of a view controller, you can replace "toolbar.items" with "self.toolbarItems")

The one catch, which I mentioned in my last post, is that you have to be sure to set the frame of the button to match the size of the image:
Code:
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
This is necessary because the button will only respond to UIEvents within its bounds, and if we don't set the frame, it will default to (0, 0, 0, 0). The reason I mentioned this is because even if you forget to set the frame, the button image is still drawn at its full size. This can be pretty confusing, because the rectangle in which you see the button being drawn on the screen doesn't actually match the button's frame, which means you can't interact with it even though you can see it (try it-- comment out the line that sets the frame, and you won't be able to tap the button even though it's still drawn).

Hope that makes things more clear. Post back if anything's confusing.
ChrisL is offline   Reply With Quote
Old 11-15-2009, 11:55 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 5
60day is on a distinguished road
Default

Quote:
Originally Posted by ChrisL View Post
Sure, no problem. UIBarButtonItem is a weird class it that it can behave in different roles depending on what properties you set. It's kind of poorly named in that while it usually behaves like a button, it can also be other things, like fixed space, etc. It can also act as a container for an arbitrary UIView, which is what we want to do here; it just happens that our custom UIView is actually a UIButton.

The code would go something like this:
Code:
//load the image
UIImage *buttonImage = [UIImage imageNamed:@"someImage.png"];
	
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
	
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
	
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Here, I'm loading the image and creating the button programmatically (in, for example, the loadView method of the view controller), but it wouldn't make much difference if you did this part in interface builder. The important line is
Code:
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
which creates a bar button item that acts as a container for a custom view; in this case, the custom view is button. (I'm not sure whether its possible to create a bar button item with a custom view in interface builder; I've never seen this option, but then I've never really looked for it).

Once this is done, you'd add it to the toolbar items array, just as you would any other bar button item. If you've never done this in code, it would go something like this
Code:
//Create an array to hold the list of bar button items
NSMutableArray *items = [[NSMutableArray alloc] init];

//add the items, including our custom button
...
[items addObject:customBarItem];
...
//set the items on the current toolbar
toolbar.items = items;
[items release];
(Here, I'm assuming you have a UIToolbar called toolbar; if your toolbar is the standard toolbar that's part of a view controller, you can replace "toolbar.items" with "self.toolbarItems")

The one catch, which I mentioned in my last post, is that you have to be sure to set the frame of the button to match the size of the image:
Code:
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
This is necessary because the button will only respond to UIEvents within its bounds, and if we don't set the frame, it will default to (0, 0, 0, 0). The reason I mentioned this is because even if you forget to set the frame, the button image is still drawn at its full size. This can be pretty confusing, because the rectangle in which you see the button being drawn on the screen doesn't actually match the button's frame, which means you can't interact with it even though you can see it (try it-- comment out the line that sets the frame, and you won't be able to tap the button even though it's still drawn).

Hope that makes things more clear. Post back if anything's confusing.
Thanks. That makes sense and really helps me out. I think it will also help solve another issue I'm dealing with. Specifically I'm trying to make the titleView of the supplied self.navigationItem clickable / touchable. I think this will help me with that as well.
60day is offline   Reply With Quote
Old 12-08-2009, 01:59 PM   #8 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 133
ghanalupo is on a distinguished road
Default

ChrisL......nice code, thanks
ghanalupo is offline   Reply With Quote
Old 01-16-2010, 04:18 AM   #9 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 9
ashish_pandita is on a distinguished road
Default

great works perfectly.

One thing was missing in that is adding a target for button?

[button addTarget:self action:@selector(testar) forControlEvents:UIControlEventTouchUpInside];
ashish_pandita is offline   Reply With Quote
Old 01-16-2010, 10:24 AM   #10 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

Quote:
Originally Posted by ashish_pandita View Post
great works perfectly.

One thing was missing in that is adding a target for button?

[button addTarget:self action:@selector(testar) forControlEvents:UIControlEventTouchUpInside];
You're correct -- the code above is just showing how to create the button and make it appear on the toolbar. You'll obviously need to set other properties as needed and add targets for the button to actually do anything useful.
ChrisL is offline   Reply With Quote
Old 01-11-2011, 08:00 AM   #11 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 173
ali.m.habib is on a distinguished road
Default

Quote:
Originally Posted by ChrisL View Post
Sure, no problem. UIBarButtonItem is a weird class it that it can behave in different roles depending on what properties you set. It's kind of poorly named in that while it usually behaves like a button, it can also be other things, like fixed space, etc. It can also act as a container for an arbitrary UIView, which is what we want to do here; it just happens that our custom UIView is actually a UIButton.

The code would go something like this:
Code:
//load the image
UIImage *buttonImage = [UIImage imageNamed:@"someImage.png"];
	
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
	
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
	
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Here, I'm loading the image and creating the button programmatically (in, for example, the loadView method of the view controller), but it wouldn't make much difference if you did this part in interface builder. The important line is
Code:
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
which creates a bar button item that acts as a container for a custom view; in this case, the custom view is button. (I'm not sure whether its possible to create a bar button item with a custom view in interface builder; I've never seen this option, but then I've never really looked for it).

Once this is done, you'd add it to the toolbar items array, just as you would any other bar button item. If you've never done this in code, it would go something like this
Code:
//Create an array to hold the list of bar button items
NSMutableArray *items = [[NSMutableArray alloc] init];

//add the items, including our custom button
...
[items addObject:customBarItem];
...
//set the items on the current toolbar
toolbar.items = items;
[items release];
(Here, I'm assuming you have a UIToolbar called toolbar; if your toolbar is the standard toolbar that's part of a view controller, you can replace "toolbar.items" with "self.toolbarItems")

The one catch, which I mentioned in my last post, is that you have to be sure to set the frame of the button to match the size of the image:
Code:
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
This is necessary because the button will only respond to UIEvents within its bounds, and if we don't set the frame, it will default to (0, 0, 0, 0). The reason I mentioned this is because even if you forget to set the frame, the button image is still drawn at its full size. This can be pretty confusing, because the rectangle in which you see the button being drawn on the screen doesn't actually match the button's frame, which means you can't interact with it even though you can see it (try it-- comment out the line that sets the frame, and you won't be able to tap the button even though it's still drawn).

Hope that makes things more clear. Post back if anything's confusing.
I used the following code t oadd a new toolbar at the buttom but it cause system crach

any suggestion to fix it
Code:
	@try {
		//create bottom toolbar using new
		toolbar = [UIToolbar new];
		toolbar.barStyle = UIBarStyleDefault;
		[toolbar sizeToFit];
		
		//Create an array to hold the list of bar button items
		NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:2];
		
		
		//Add buttons
		
		//load the image
		UIImage *buttonImage = [UIImage imageNamed:@"pan.png"];
		//create the button and assign the image
		UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
		[button setImage:buttonImage forState:UIControlStateNormal];
		//set the frame of the button to the size of the image (see note below)
		button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
		//create a UIBarButtonItem with the button as a custom view
		WindowWidthZoom = [[UIBarButtonItem alloc] initWithCustomView:button];
		[items addObject:WindowWidthZoom ];
		
		
		UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
																				  target:nil
																				  action:nil];
		[items addObject:flexItem];
		
		toolbar.items = items;
		[items release];
		
		
		//release buttons
		[WindowWidthZoom release];
		
		//[SendMail release];
		
		[flexItem release];
		
		//add array of buttons to toolbar
		//[toolbar setItems:items ];
		
		/* let it be visible  */
		[self setToolbarItems:items];
		[[self navigationController] setToolbarHidden:NO animated:NO]; // application crach here give messege EXC_BAD_ACCESS
		
		
	}
	@catch (NSException * e) {
		
		
		 
	}
	@finally {
		 
	}
application crach here give messege EXC_BAD_ACCESS
ali.m.habib is offline   Reply With Quote
Old 04-29-2011, 05:23 AM   #12 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 2
Balakrishnan is on a distinguished road
Default toolbar Item added crash

[code]
@try {
//create bottom toolbar using new
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];

//Create an array to hold the list of bar button items
NSMutableArray *items = [[NSMutableArray alloc] init];


//Add buttons

//load the image
UIImage *buttonImage = [UIImage imageNamed:@"pan.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
//create a UIBarButtonItem with the button as a custom view
WindowWidthZoom = [[UIBarButtonItem alloc] initWithCustomView:button];
[items addObject:WindowWidthZoom ];


UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemF lexibleSpace
target:nil
action:nil];
[items addObject:flexItem];

toolbar.items = items;
[items release];


//release buttons
[WindowWidthZoom release];

//[SendMail release];

[flexItem release];

//add array of buttons to toolbar
//[toolbar setItems:items ];

/* let it be visible */
[self setToolbarItems:items];
[[self navigationController] setToolbarHidden:NO animated:NO]; // application crach here give messege EXC_BAD_ACCESS


}
@catch (NSException * e) {



}
@finally {

}
Its simple You given array length 2 and try to add more than 2 items in NSMutableArray
Balakrishnan is offline   Reply With Quote
Old 04-29-2011, 05:54 AM   #13 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 2
Balakrishnan is on a distinguished road
Default

[quote=Balakrishnan;329182][code]
[code]
@try {
//create bottom toolbar using new
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];

//Create an array to hold the list of bar button items
NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:10];


//Add buttons

//load the image
UIImage *buttonImage = [UIImage imageNamed:@"pan.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem* WindowWidthZoom = [[UIBarButtonItem alloc] initWithCustomView:button];
[items addObject:WindowWidthZoom ];


UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemF lexibleSpace
target:nil
action:nil];
[items addObject:flexItem];
UIBarButtonItem* WindowWidthZoom1 = [[UIBarButtonItem alloc] initWithCustomView:button];
[items addObject:WindowWidthZoom1 ];
//toolbar.items = items;
[toolbar setItems:items animated:NO];
[items release];


//release buttons
[WindowWidthZoom release];

//[SendMail release];

[flexItem release];

//add array of buttons to toolbar


/* let it be visible */
[self.window addSubview:toolbar];

}
@catch (NSException * e) {



}
@finally {

}
Its simple You given array length 2 and try to add more than 2 items in NSMutableArray
[self setToolbarItems:items]; is invalid function it used for adding array to toolbar not to view directly and the method also wrong the correct method is - (void)setItemsNSArray *)items animatedBOOL)animated ie. [toolbar setItems:items animated:NO];


[self.window addSubview:toolbar]; or [[self navigationController] addSubview:toolbar]; is added then it works fine
Balakrishnan is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 339
13 members and 326 guests
akacaj, alexP, ClerurcifeDer, Duncan C, givensur, glenn_sayers, GraffitiCircus, guusleijsten, JmayLive, NetGuru, Paul Slocum, Punkjumper, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:44 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0