jchitel
jchitel

Reputation: 3169

iOS Core Graphics: How to properly set and use colors

I recently started developing an iOS app, something I've never done before. I have a lot of experience with CSS styling, so trying to work with graphics objects and libraries is quite foreign to me.

A lot of the tutorials I've seen use a CGGraphicsContext to draw to the screen. However, I am trying to draw a rounded rectangle, and the answers I've seen on how to do that involve something like this (BTW this is Swift, not Objective-C. The classes are all the same, the syntax is different):

let buttonRect: UIBezierPath = UIBezierPath(roundedRect: rect, cornerRadius: 5.0)
buttonRect.fill()

I implement this code in the drawRect() method of my UIButton subclass, with this code preceding:

let color: UIColor = UIColor(red: 190, green: 190, blue: 190, alpha: 1.0)
color.setFill()

Based on what I know, calling setFill() on a color should set the fill color of the underlying graphics to that color, and any following calls to fill() will use that color.

When I run this code, the rounded rectangle pops up just as I expect, but it's white, not the color I specified.

Is there something I'm missing. I'm not sure how custom drawing integrates with the options in the interface builder, would that have something to do with it?

Upvotes: 0

Views: 220

Answers (1)

imas145
imas145

Reputation: 1979

UIColor RGB values range from 0.0 to 1.0. You should replace this line

let color: UIColor = UIColor(red: 190, green: 190, blue: 190, alpha: 1.0)

with this:

let color: UIColor = UIColor(red: 190.0/255.0, green: 190.0/255.0, blue: 190.0/255.0, alpha: 1.0)

Upvotes: 1

Related Questions