UIColor colorWithRed:green:blue:alpha: always returns white unless one argument is 0

别等时光非礼了梦想. 提交于 2019-12-28 23:05:33

问题


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?


回答1:


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]; 



回答2:


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

Try:

[UIColor colorWithRed:136.0/255.0 green:155.0/255.0 blue:218.0/255.0 alpha:1.0]; 



回答3:


The params are not integers but floats, so you probably want to divide all color float values with 255. Like this:

  [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.




回答4:


Set navigation bar to transparent in iOS 11

(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.view.backgroundColor = [UIColor clearColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
}


来源:https://stackoverflow.com/questions/7584531/uicolor-colorwithredgreenbluealpha-always-returns-white-unless-one-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!