CGTheLegend
CGTheLegend

Reputation: 774

iOS 7 colorWithAlphaComponent error

So I am trying to have my button's border change color when pressed and I'm finding some issues. This is what I have:

UIColor *blackColor;
UIColor *transBlack = [blackColor colorWithAlphaComponent:05f];
self.layer.borderColor = [UIColor transBlack].CGColor;

Now the last line is giving me an error which reads "No known class method for selector 'transBlack' " and "Property 'CGColor' not found on object of type 'id' " I have no idea what either of these mean. I'd like to get that last line to work, and if you could explain to me why the compiler is complaining that would be very helpful.

Any and all help would be greatly appreciated.

Edit: So I tried using a different method

colorWithHue:0 saturation:0 brightness: 0 alpha: 0.5

and that seems to have broken my button's push outlet. I'm not sure why yet.

Edit2:

This seemed to correct the original issue of using colorWithAlphaComponent

UIColor *transBlack = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
self.layer.borderColor = transBlack.CGColor;

for more information please look at the selected answer.

Upvotes: 2

Views: 1299

Answers (1)

Ron
Ron

Reputation: 1269

There are two problems that I can see. First you are creating a variable "blackColor" but are not assigning anything to it (so it will be nil). I think what you meant was to call the class method blackColor on UIColor (allocates and initializes a UIColor instance). The second problem is [UIColor transBlack]. There is no selector on UIColor called transBlack. You just created a variable called transBlack which happens to be an instance of UIColor... So just get the CGColor from transBlack directly.

Something like this:

UIColor *transBlack = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
self.layer.borderColor = transBlack.CGColor;

Upvotes: 5

Related Questions