iOS , Changing view background colour using stepper

702 views Asked by At

I want to change the background of view using rgb values obtained from stepper for some reason it isn.t working,please help!

int red1,green1,blue1;

 -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        return NO;
    }
    -(IBAction)stepper:(UIStepper *)mystep
    {
        if(mystep.tag==1)
        {
            red1=(int)mystep.value;
            txt1.text=[NSString stringWithFormat:@"%d",red1];
        }
        if(mystep.tag==2)
        {
            green1=(int)mystep.value;
            txt2.text=[NSString stringWithFormat:@"%d",green1];
        }
        if(mystep.tag==3)
        {
            blue1=(int)mystep.value;
            txt3.text=[NSString stringWithFormat:@"%d",blue1];
        }
    }
    -(IBAction)btnPressed
    {
        view1.backgroundColor=[UIColor colorWithRed:red1 green:green1 blue:blue1 alpha:1.0];
    }
1

There are 1 answers

1
Tushar On

The parameters in the colorWithRed:green:blue:alpha: method should be in the range of 0.0 to 1.0 (CGFloat) So in case you are taking values in the range of 0 to 255, then change the code to:

view1.backgroundColor=[UIColor colorWithRed:red1/255.0 green:green1/255.0 blue:blue1/255.0 alpha:1.0];