Iphone UIButton Background Color

I create the program code UIButtonin a loop, but I had a problem with setting the background color of the button.

The color of the button is always displayed as white. But it works fine, I use only 2 colors in the backgroundcolor. For example: red: 255 green: 0 blue: 200

Here is the code I use to add a 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];
+5
source share
2 answers

I believe that you are building your UIColor incorrectly.

Try the following:

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

accepts CGFloats between 0.0 and 1.0

Here is the api link.

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

+2

All Articles