我正在尝试更改类别文件中的按钮颜色,但我不知道怎么办?

I am trying to change button colour in category file but there is no idea for me?

这是十六进制转RGB的代码。一切正常。

 #import "UIButton+ButtonClr.h"

@implementation UIButton (ButtonClr)
- (UIColor *)colorFromHexString:(NSString *)hexString {
    unsigned rgbValue = 0;
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    [scanner setScanLocation:1]; /// bypass '#' character///
    [scanner scanHexInt:&rgbValue];
    return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];

}
-(void)btnclr
{
    UIButton *set1;//i tried many methods here but no use please provide some help full method


}
///set1.backgroundcolor=[self colorFromHexString:@"#ffc400" ];///here i am used this for changing the color but not working.

UIColor+HexColor.h

@interface UIColor (HexColor)
// send hex color codes array and it will return UIColor
+ (UIColor *)colorWithHexArray:(NSString *) color;
@end 

UIColor+HexColor.m

 + (UIColor *)colorWithHexArray:(NSString *) color
        {
            @try {
                NSMutableArray * rgbColorArray = [[NSMutableArray alloc] init];


                    NSString * colorArray1 = [NSString stringWithFormat:@"%@",color];

                    NSString *cString = [[colorArray1 stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];


                    // String empty then show clear color
                    if ([cString length]==0) return [UIColor clearColor];


                    // String should be 6 or 8 characters
                    if ([cString length] < 6) return [UIColor grayColor];

                    // strip 0X if it appears
                    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];

                    // # replace with blank space
                    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];


                    if ([cString length] != 6) return  [UIColor grayColor];

                    // Separate into r, g, b substrings
                    NSRange range;
                    range.location = 0;
                    range.length = 2;
                    NSString *rString = [cString substringWithRange:range];

                    range.location = 2;
                    NSString *gString = [cString substringWithRange:range];

                    range.location = 4;
                    NSString *bString = [cString substringWithRange:range];

                    // Scan values
                    unsigned int r, g, b;
                    [[NSScanner scannerWithString:rString] scanHexInt:&r];
                    [[NSScanner scannerWithString:gString] scanHexInt:&g];
                    [[NSScanner scannerWithString:bString] scanHexInt:&b];

                    UIColor * color = [UIColor colorWithRed:((float) r / 255.0f)
                                                      green:((float) g / 255.0f)
                                                       blue:((float) b / 255.0f)
                                                      alpha:1.0f];

                    [rgbColorArray addObject:color];
                    return [rgbColorArray objectAtIndex:0];
                }

                return [UIColor clearColor];
            }
            @catch (NSException *exception) {
                NSLog(@"Exception %@",exception);
            }
            @finally {

            }
            return [UIColor clearColor];
        }

按如下方式调用该类别方法:

set1.backgroundColor =  [UIColor colorWithHexArray:@"#ff2323"];

我希望它能解决你的问题:)