Reputation: 1040
I am creating several classes for theme-ing support in my iOS 5 app. My themes are stored in plist and I load them up in a Theme object, which I use in my app to initialize various controls. I store the colors as strings in my theme and then I use this code to convert them to UIColor:
UIColor* color = [UIColor colorWithCIColor:[CIColor colorWithString:@"0.5 0.5 0.5 1.0"]];
This works fine for most controls, however when I try to set the tint color of the navigation bar as such:
//navigation bar
[self.navigationController.navigationBar setTintColor:color];
I get this exception:
-[UICIColor colorSpaceName]: unrecognized selector sent to instance
When I initialize the color without using CIColor e.g. like this:
UIColor* color = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
[self.navigationController.navigationBar setTintColor:color];
All works great.
Any clues what is causing this? I could not find much info about UICIColor, but I am guessing since UIColor is only a wrapper on top of CGColor or CIColor there are implementation differences.
Upvotes: 5
Views: 2516
Reputation: 6093
I had similar issues with
[UIColor colorWithCIColor:[CIColor colorWithString:color]];
Although I looked for an elegant fix for this, in the end I settled with a solution that stopped the problem and enabled my app to continue exactly as before.
My color string was in the same format as yours:
"0.5 0.7 0.2 0.75"
I found the easiest way to fix it was just to do the following:
NSArray * colorParts = [color componentsSeparatedByString: @" "];
CGFloat red = [[colorParts objectAtIndex:0] floatValue];
CGFloat green = [[colorParts objectAtIndex:1] floatValue];
CGFloat blue = [[colorParts objectAtIndex:2] floatValue];
CGFloat alpha = [[colorParts objectAtIndex:3] floatValue];
UIColor * newColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
Manually splitting out each value and then putting then into the colorWithRed: code.
It means you can keep your color string but get rid of the problematic colorWithString code which is causing all the crashes.
Hope this helps
Upvotes: 0
Reputation: 21
See Strange crash when I try to access to uibutton's titleLabel property (xcode 4.5 and IOS sdk 6.0)
I found a workaround : before using my colorWithCIColor, I made a copy of it with :
newcolor = [UIColor colorWithCGColor:newcolor.CGColor];
and it solves the crash. Strange, anyway
Upvotes: 2
Reputation: 11
UIColor
define in iOS 2.0
,because of [UIColor colorWithCIColor]
convert to iOS5.0
, I think that apple convert error, you can using below code:
CIColor *ci_ = [CIColor colorWithString:colorString];
UIColor *color = [UIColor colorWithRed:ci_.red green:ci_.green blue:ci_.blue alpha:ci_.alpha];
// UIColor *color = [UIColor colorWithCIColor:[CIColor colorWithString:colorString]];
Upvotes: -1