New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Changing entire button background color


Post Reply New Thread Subscribe

 
Thread Tools
SoulRed12

 
Member Since: Mar 04, 2010
Posts: 49
SoulRed12 is on a distinguished road

SoulRed12 is offline
I just have a quick question.

I'm writing a tic tac toe game for the heck of it (practice, I guess) and I want to make it so that when a player gets three in a row, it highlights the three buttons in some color. I can get the center rectangle of the buttons to change color but I can't get the entire button to change color like for example when you click on it. Here:



See? I'd like the buttons to nicely highlight the entire button but what they do now is just a rectangle in the middle and ignore the parts on the left and right. Note the buttons are just normal UIButtons (RoundRectButtons) created and placed in the view through code. Here's the code I used to do the highlighting:

Code:
- (void)highlightSpaces:(int)one second:(int)two third:(int)three {
	if (one<9 && two<9 && three<9) {
		//Note the 9 tic tac toe buttons are contained in one NSMutableArray
		[[[buttonsArray objectAtIndex:one] titleLabel] setBackgroundColor:[UIColor greenColor]];
		[[[buttonsArray objectAtIndex:two] titleLabel] setBackgroundColor:[UIColor greenColor]];
		[[[buttonsArray objectAtIndex:three] titleLabel] setBackgroundColor:[UIColor greenColor]];
	}
}
Any ideas would be appreciated. Thanks!
QUOTE Thanks
nabl

 
nabl's Avatar
 
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork 09

nabl is offline
To change the color of a UIButton, I believe you have to use UIButtonTypeCustom (in Interface Builder choose Custom for Type in the Attributes Inspector)—there's no way I know of to change the UIButtonTypeRoundedRect colors. Once you use a custom button, you can use a background image for the buttons, or set the actual backgroundColor property of the UIButton if you don't use an image.

For example,
Code:
- (void)highlightSpaces:(int)one second:(int)two third:(int)three {
	if (one<9 && two<9 && three<9) {
		//Note the 9 tic tac toe buttons are contained in one NSMutableArray
		[[buttonsArray objectAtIndex:one] setBackgroundColor:[UIColor greenColor]];
		[[buttonsArray objectAtIndex:two] setBackgroundColor:[UIColor greenColor]];
		[[buttonsArray objectAtIndex:three] setBackgroundColor:[UIColor greenColor]];
	}
}
If you don't use a background image, the buttons will have whichever background color you set for them either in your view initialization or in Interface Builder until you run this method (unless you change them elsewhere), so there won't be much feedback for the user upon tapping the buttons—only the button labels will animate.

If you use images, you simply need to set the correct image for each state, which is easy either way. In Interface Builder, you simply choose from the State Configuration pop-up button which state you're changing and type in the image name (after adding it to your XCode project). In code, you'll use the setBackgroundImage:forState: method (the states are in the documentation for UIButton). In your case, you could perhaps use the green highlight image for the selected state and then set the buttons to selected at the end of the game.
Code:
- (void)highlightSpaces:(int)one second:(int)two third:(int)three {
	if (one<9 && two<9 && three<9) {
		//Note the 9 tic tac toe buttons are contained in one NSMutableArray
		[[buttonsArray objectAtIndex:one] setSelected:YES];
		[[buttonsArray objectAtIndex:two] setSelected:YES];
		[[buttonsArray objectAtIndex:three] setSelected:YES];
	}
}
One final note: I'd recommend not naming variables after actual numbers. It's very confusing to think that the integer variable one could really be holding the number 42. Perhaps firstIndex, secondIndex, and thirdIndex (or something similar) would be better choices.
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
The limited research I did on this suggested the only method was the use of an image as nabl mentions.

I did do a little project where I created a color image, via code, to match the size of the button, but when I set it as the background, it filled in the corners too. I don't know an easy way around that problem. I think you'd have to draw your color inside a circular area.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks
SoulRed12

 
Member Since: Mar 04, 2010
Posts: 49
SoulRed12 is on a distinguished road

SoulRed12 is offline
Alright, thanks. I'll look into using images.
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
I just found "Fun With UIButtons and Core Animation Layers" over at Cocoa is My Girlfriend.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks

Post Reply New Thread Subscribe


« "Expected expression before 'else'" problem | Cilk Language Intallation »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Changing OS X Sleep Login background SBDTHRU OS X - Operating System 0 04-02-2010 12:23 PM
changing default os focus button dralion OS X - Operating System 4 02-18-2009 06:48 AM
Changing background laddie Switcher Hangout 2 01-27-2009 06:12 PM
Changing the screen saver authenticate background ivytwines OS X - Operating System 1 12-28-2008 12:33 AM
Changing DW's dropdown nav background from solid color to an image? Morgan19 Web Design and Hosting 4 02-01-2006 02:48 PM

All times are GMT -4. The time now is 04:47 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?