Results 1 to 5 of 5
  1. #1
    Changing entire button background color

    Member Since
    Mar 04, 2010
    Posts
    50
    Changing entire button background color
    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!

  2. #2
    Changing entire button background color
    nabl's Avatar
    Member Since
    Dec 13, 2007
    Location
    United States of America
    Posts
    256
    Specs:
    2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork 09
    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.

  3. #3
    Changing entire button background color
    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    Specs:
    2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
    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.

  4. #4
    Changing entire button background color

    Member Since
    Mar 04, 2010
    Posts
    50
    Alright, thanks. I'll look into using images.

  5. #5
    Changing entire button background color
    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    Specs:
    2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Safari Background Color
    By dumbmonkey in forum OS X - Operating System
    Replies: 3
    Last Post: 06-16-2014, 07:01 AM
  2. Changing background color of finder
    By chourreau in forum OS X - Operating System
    Replies: 2
    Last Post: 11-25-2011, 05:45 PM
  3. Background color
    By TOYOTA in forum Apple Notebooks
    Replies: 1
    Last Post: 03-19-2006, 02:39 PM
  4. Replies: 1
    Last Post: 02-23-2006, 11:03 PM
  5. Changing DW's dropdown nav background from solid color to an image?
    By Morgan19 in forum Web Design and Hosting
    Replies: 4
    Last Post: 02-01-2006, 02:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •