UIColor colorWithRed:green:blue:alpha: always returns white unless one argument is 0
I am using
[UIColor colorWithRed:136 green:155 blue:218 alpha:1.0];
to change the background color of my table view cells.
But for some reason the background just stays white. If I try
[UIColor colorWithRed:0 green:155 blue:218 alpha:1.0];
the color changes. This does not make sense to me. Any advice or tips?
3 Solutions Collect From Internet About “UIColor colorWithRed:green:blue:alpha: always returns white unless one argument is 0”
The parameter aren’t a values of 0 to 255 but a float between 0.0 and 1.0:
[UIColor colorWithRed:0.0f/255.0f green:155.0f/255.0f blue:218.0f/255.0f alpha:1.0f];
The values need to be floats (in the range 0.0 to 1.0), not ints (in the range 0 to 255).
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
[UIColor colorWithRed:136.0/255.0 green:155.0/255.0 blue:218.0/255.0 alpha:1.0];
The params are not integers but floats, so
you probably want to divide all color float values with 255.
[UIColor colorWithRed:136.0/255 green:155.0/255 blue:218.0/255 alpha:1.0];
That’s why the color changes when you set red to 0 instead of 1 which 136 means in this case.
- How to determine UIWebView height based on content, within a variable height UITableView?
- CLOCKS_PER_SEC Not Matching Results From std::clock()
- Determine which share extension was used
- Bundle framework with application in XCode
- How to get the bigger profile picture of a facebook page
- Detecting taps on attributed text in a UITextView while the keyboard is showing
- Xcode 8 shows error that provisioning profile doesn't include signing certificate
- Why system call UIApplicationDelegate's dealloc method?
- Why optional constant does not automatically have a default value of nil
- How to efficiently use Autolayut and Auto-Resizing in same target?
- Recommended way to declare delegate properties with ARC
- Objective-C getter/ setter
- setSelectedImageTintColor not working in iOS 7
- UISwitch in accessory view of a tableviewcell, passing a parameter with the selector to have selector function see the indexpath.row?
- Terminating app due to uncaught exception 'NSUnknownKeyException'