0

I want to place a series of images in a UIView... I want to be able to change each image's color using three different .png files (red,yellow,green) as a status indicator. Other than actually drawing the rectangle and filling it, is there an easier way?

Research effort: I have Googled and looked through SO and found nothing dealing with this.

SpokaneDude
  • 4,576
  • 11
  • 61
  • 116
  • I didn't downvote this question, but the question doesn't show a lack of effort, but it would be better--or disappear altogether--if you break it down. Do you know how to display a PNG in a view? Do you know how to place a view in a view? Do you know how to change the color of a view, or change the color of an image? Which of these are relevant for your question, etc. But anyway, good luck on SO and getting an answer to your question. – Dan Rosenstark Aug 27 '12 at 23:17

2 Answers2

1

There are lots of ways you can do this. I think the easiest way would be to pragmatically add UIViews as subviews to your main view, or to your parent UIView.

You can set the frame of your views as well as the background color. See this link

adding uiview as a subview to the main view

You can also use images, but since it is a plain solid color, its a lot of extra storage space, etc. to use images when you can programatically render a color.

If it is a series of rectangles, you can store UIViews in some kind of data structure so it is easier to dynamically change the color at runtime. If you aren't moving them around, then init there frames/geometry so they are in the correct location, then access them as members of an array or something similar.

If you want to add gradients to images, this is the best thing I have found:

Gradients on UIView and UILabels On iPhone

Community
  • 1
  • 1
FaddishWorm
  • 5,278
  • 9
  • 32
  • 41
  • Right - if your are happy with square or rectangles you can create a UIView, then set the background color (red, green, blue, or clear). – David H Aug 27 '12 at 23:12
  • It's not a plain solid color... it's like the upper left hand corner of OS X's windows... red, yellow, green balls. – SpokaneDude Aug 27 '12 at 23:13
  • you can add tints etc programatically, this isn't directly related but you might be able to pull some code from here: http://stackoverflow.com/questions/571028/changing-tint-background-color-of-uitabbar – FaddishWorm Aug 27 '12 at 23:16
  • Please check the final link in my answer :) – FaddishWorm Aug 27 '12 at 23:17
0

I figured it out... I took a UIButton, changed the Type to "Custom", sized it (32 x 32) and programmatically changed the the image depending on circumstances.

  • [button setImage:<#(UIImage *)#> forState:<#(UIControlState)#>]

Thanks to both of you for getting the thought process running... :D

SpokaneDude
  • 4,576
  • 11
  • 61
  • 116