2711月/11Off
navigationItemのUIBarButtonItemを好きな色にする
チャオ!
iOS5からはUIBarButtonItemにもtintColorが設定できるようになって超めでたしめでたしと思っていたらiOS4で死ねと言われて使えねーって状態になりました。
そこで画像とか用意しないで好きな色のボタン置けないかと探してたらいい方法が!
以下な感じでやればOKです!
- (void)viewDidLoad { [super viewDidLoad]; UIColor *color = [UIColor colorWithRed:0.30 green:0.70 blue:0.00 alpha:1.0]; self.navigationItem.rightBarButtonItem = [self barButtonItemWithTint:color title:@"test" target:self selector:@selector(test:)]; } -(UIBarButtonItem *)barButtonItemWithTint:(UIColor *)color title:(NSString *)title target:(id)target selector:(SEL)selector { UISegmentedControl *btn = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:title, nil]] autorelease]; btn.momentary = YES; btn.segmentedControlStyle = UISegmentedControlStyleBar; btn.tintColor = color; [btn addTarget:target action:selector forControlEvents:UIControlEventValueChanged]; UIBarButtonItem *barBtn = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease]; return barBtn; }
・・・・鬼の見づらさですね・・・
ようはUIBarButtonItemにtintColorのあるsegmentedControlをつっこんで、代わりにしようねっ♪って感じです。
ただし角丸の通常ボタンしか駄目なので、戻るとかはやっぱ画像がいるんだろうな・・・
UIBarButtonItemを作ってくれる関数はどこからでも呼べるようにしているといいかと思います!
以下のサイトから学びました!
How to change the color of a UIBarButtonItem
1411月/11Off
Objective-Cでの色設定を簡単に行うサービスを作ったよ
チャオ!
Objective-Cで色を作るときに、Photoshopから取った色をいちいち255で割って入力することに疲れたのでそれ用のサービスを作りました!
以下を設定しておくと色々楽できるのですが、
設定 #define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1] 使う時 text.color = RPG(255, 0, 0);
グラデーションのとかで、どーしても色がいるんだよ!!とかあるので作りました。
f00 ff0
とか改行で複数入れると複数の色を返してくれるよ!
あとヒストリーに残るので、また使いたい!とかのとき便利なはず!
それじゃ!