ログイン



tintColor

金曜日, 11月 18th, 2011 by

iOS5から、UIViewのサブクラスの幾つかでtintColor属性が使えるようになった。UINavigationBarや、UIToolBarで使えるようになっているのは間違いないが、それ以外のどのクラスで使えるのか調査してみた。

1. ヘッダーを調査

公式ドキュメントでtintColorという文字列を検索する方法がわからなかったので、headerファイルを調査した。tintColorが含まれているproperty行は以下の7つあった。おそらくこの7つのクラス(とそのサブクラス)で使えるのだろう。7つものクラスで独立して定義されているのは違和感を感じるが(プロトコルとかを使うべき?)、得られたクラスのリストは妥当そうだ。

macbookpro:Headers mac$ pwd
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/System/Library/Frameworks/UIKit.framework/Headers
macbookpro:Headers mac$ grep tintColor * | grep @property
UIBarButtonItem.h:@property(nonatomic,retain) UIColor *tintColor __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;
UIButton.h:@property(nonatomic,retain)   UIColor     *tintColor __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0); // default is nil. only valid for some button types
UINavigationBar.h:@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
UISearchBar.h:@property(nonatomic,retain) UIColor                *tintColor;             // default is nil
UISegmentedControl.h:@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
UITabBar.h:@property(nonatomic,retain) UIColor *tintColor __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;
UIToolbar.h:@property(nonatomic,retain) UIColor   *tintColor  UI_APPEARANCE_SELECTOR;

この結果から、tintColor属性を持つクラスは以下の7つだとわかる。

  • UIBarButtonItem
  • UIButton
  • UINavigationBar
  • UISearchBar
  • UISegmentedControl
  • UITabBar
  • UIToolbar

2. 使ってみた

想定通りの動作をしている。

  • UINavigationBar + UIBarButtonItem
    UIViewController * vc = [[UIViewController alloc] init];
    vc.title = @"Title";
    UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
    nc.navigationBar.tintColor = [UIColor redColor];
    UIBarButtonItem * bbi1 = [[UIBarButtonItem alloc] initWithTitle:@"info" style:UIBarButtonItemStylePlain target:nil action:nil];
    UIBarButtonItem * bbi2 = [[UIBarButtonItem alloc] initWithTitle:@"info" style:UIBarButtonItemStylePlain target:nil action:nil];
    bbi2.tintColor = [UIColor grayColor];
    vc.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:bbi1, bbi2, nil];

  • UIToolbar + UIBarButtonItem
    UIViewController * vc = [[UIViewController alloc] init];
    UIToolbar * tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    [vc.view addSubview:tb];
    tb.tintColor = [UIColor orangeColor];
       
    UIBarButtonItem * bbColored = [[UIBarButtonItem alloc] initWithTitle:@"UIBarButtonItem" style:UIBarButtonItemStyleBordered target:nil action:nil];
    bbColored.tintColor = [UIColor grayColor];
       
    UIBarButtonItem * bbNotColored = [[UIBarButtonItem alloc] initWithTitle:@"UIBarButtonItem" style:UIBarButtonItemStyleBordered target:nil action:nil];
    tb.items = [NSArray arrayWithObjects:bbColored, bbNotColored, nil];

まとめ

iOS5から導入されたtintColorを使うことで、手軽に標準コンポーネントの表示色を変更できることを確認できた。