2016-06-11 11 views
0

ピッカー表示の背景に3色の異なる色を設定したい 現在のピッカー(選択した項目)にある項目のバックグラウンド項目を1つ選択します。それ。ピッカーの背景色で異なる色を設定する方法

私はカラー(#01445)の16進数を持っているので、ピッカーの背景に設定する必要があります。これらの16進値を設定する方法。 また、このカラー(#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

に彼女の色を使用するために作成した関数である私は、あなたがfromHex方法でUIColorのカテゴリが必要であることを考えると、これは私があなたと共有したいものです

これは.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

実際に私はすでに16進数の色を設定する関数を作成しましたが、更新されたコードを見てください。しかし、今私の問題は私がピッカーから選択した行の背景色を実装したかったことです。 – vicky

+0

私の答えは16時間前でした;)、今あなたのセルバックグラウンドの色を変更するために変更する必要がありますか? –

+0

あなたはどうすればいいのでしょうか?@Reinier Melian – vicky

関連する問題