2016-06-11 66 views
0

我想设置三种不同的颜色来选择器视图背景 一个用于背景项目一个用于当前选择器(选定项目)的项目。我该怎么做那。如何在Picker背景中设置HEX值不同的颜色

我有一个颜色的十六进制值(#01445),所以我必须将其设置为选取器背景。如何设置这些十六进制值。 而且我还必须将此颜色(#014455)值设置为选取器中的选定项目。

myPickerView = [[UIPickerView alloc]init]; 
myPickerView.dataSource = self; 
myPickerView.delegate = self; 
myPickerView.showsSelectionIndicator = YES; 
myPickerView.tag=1; 
[myPickerView setBackgroundColor:[self colorFromHexString:@"#014455"]]; //set different color here with hex value 

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
           initWithTitle:@"Done" style:UIBarButtonItemStyleDone 
           target:self action:@selector(done:)]; 


UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame: 
         CGRectMake(0, self.view.frame.size.height- 
           myPickerView.frame.size.height-(self.view.frame.size.width/6.4),self.view.frame.size.width,50)]; 
[toolBar setBarStyle:UIBarStyleBlackOpaque]; 

[toolBar setBackgroundColor:[self colorFromHexString:@"#ff7a03"]]; // i have hex value #ff7a03 so i have to set here 

NSArray *toolbarItems = [NSArray arrayWithObjects: 
         doneButton, nil]; 
[toolBar setItems:toolbarItems]; 

这是我使用的背景她的颜色创建函数

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

回答

0

我认为你需要的UIColor一类具有fromHex方法,这就是我想和你

分享什么

这是.H

#import <UIKit/UIKit.h> 

@interface UIColor (Hex) 

+(UIColor*)colorFromHex:(NSString*)hex; 

@end 

是是.M

#import "UIColor+Hex.h" 

@implementation UIColor (Hex) 

+(UIColor*)colorFromHex:(NSString*)hex 
{ 
    NSString * cString = [hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].uppercaseString; 

    if([cString hasPrefix:@"#"]) 
    { 
     cString = [cString substringFromIndex:1]; 
    } 

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

    NSString * rString = [cString substringToIndex:2]; 
    NSString * gString = [cString substringWithRange:NSMakeRange(2, 2)]; 
    NSString * bString = [cString substringWithRange:NSMakeRange(4, 2)]; 

    unsigned int r,g,b = 0; 

    [[[NSScanner alloc]initWithString:rString] scanHexInt:&r]; 
    [[[NSScanner alloc]initWithString:gString] scanHexInt:&g]; 
    [[[NSScanner alloc]initWithString:bString] scanHexInt:&b]; 

    return [UIColor colorWithRed:(CGFloat)r/255.0 green:(CGFloat)g/255.0 blue:(CGFloat)b/255.0 alpha:1]; 
} 


@end 

,然后你只需要的是导入此的UIColor + Hex.h和使用这种方法

这样

myPickerView = [[UIPickerView alloc]init]; 
myPickerView.dataSource = self; 
myPickerView.delegate = self; 
myPickerView.showsSelectionIndicator = YES; 
myPickerView.tag=1; 
myPickerView.backgroundColor =[UIColor colorFromHex:@"#014455"]; //set different color here with hex value 

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
           initWithTitle:@"Done" style:UIBarButtonItemStyleDone 
           target:self action:@selector(done:)]; 


UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame: 
         CGRectMake(0, self.view.frame.size.height- 
           myPickerView.frame.size.height-(self.view.frame.size.width/6.4),self.view.frame.size.width,50)]; 
[toolBar setBarStyle:UIBarStyleBlackOpaque]; 

[toolBar setBackgroundColor:[UIColor colorFromHex:@"#ff7a03"]]; // i have hex value #ff7a03 so i have to set here 

NSArray *toolbarItems = [NSArray arrayWithObjects: 
         doneButton, nil]; 
[toolBar setItems:toolbarItems]; 

我希望这有助于你修改代码

+0

其实我已经创建了设置十六进制颜色的功能,请参阅更新的代码,但现在我的问题是我想实现我们从选择器中选择的行的背景颜色。 – vicky

+0

好,但我的答案是16小时前;),你需要改变现在改变你的细胞背景颜色? –

+0

你能告诉我我该怎么做@Reinier Melian – vicky