Iphone UIButton background color

*爱你&永不变心* 提交于 2020-01-22 15:15:09

问题


I'm creating some UIButtons programmatically in a loop but I'm having some problem with setting the background color of the button.

The color of the button always shows up as white. But works fine with I'm only using 2 colors in the backgroundcolor. Eg : red : 255 green:0 blue :200

Here is the code I'm using to add the button.

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(80, 20 + (i*75), 200, 75);
    button.layer.cornerRadius = 10;
    button.layer.borderWidth = 1;
    [button setTitle:@"saf" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(moveLocation:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundColor:[UIColor colorWithRed:255 green:180 blue:200 alpha:1]];
    button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [scrollView addSubview:button];

回答1:


I believe you are building your UIColor wrong.

Try this:

[button setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(180/255.0) blue:(200/255.0) alpha:1]];



回答2:


UIColor colorWithRed: green: blue

accepts CGFloats between 0.0 and 1.0

Here is the api reference.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIColor_Class/Reference/Reference.html



来源:https://stackoverflow.com/questions/6591281/iphone-uibutton-background-color

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