「UIButton」カテゴリーアーカイブ

UIButtonの背景色を変更する


UIButtonTypeRoundedRectタイプのUIButtonは、すっきりしていて使いやすいのですが、背景色ぐらいは設定したいですよね。でも出来ません。\(^o^)/。背景色を設定したい場合は、UIButtonTypeCustomで無色透明なボタンを作成しておき、背景と枠を自力で描画する必要があります。といっても以下のコードをコピペすればOKです。

1. ヘッダーでQuartzCore/QuartzCore.hをインポートします。

#import <QuartzCore/QuartzCore.h>

2. UIButtonTypeCustomタイプのボタンを生成し枠をつけます。

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50,50,100,100);
[self.view addSubview:button];
[[button layer] setCornerRadius:8.0f];
[[button layer] setMasksToBounds:YES];
[[button layer] setBorderWidth:1.0f];
[[button layer] setBackgroundColor:
    [[UIColor colorWithRed:1.0 green:0.8 blue:0.8 alpha:1.0] CGColor]];
動作イメージ
動作イメージ

サンプルプロジェクト: Button

参考:
http://stackoverflow.com/questions/4909637/uibutton-color-issues

クラスごとのtintColor実装バージョン


iOS5.0以降では、tintColorを利用できるクラスが増えました。iOS4系以前との互換性を保つためには、新規導入されたtintColorを使わないか、tintColorが実装されていない場合の代替処理を記述しておく必要があるので注意が必要です。

iOS 5.0以降

以下の3つのクラスではiOS5.0以降でtintColorを設定できます。

  • UIBarButtonItem
  • UIButton
  • UITabBar

    iOS2.0以降

    以下の4つのクラスではiOS2.0以降でtintColorを設定できます。

  • UINavigationBar
  • UISearchBar
  • UISegmentedControl
  • UIToolbar
  • tintColor


    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を使うことで、手軽に標準コンポーネントの表示色を変更できることを確認できた。

    UIButtonのラベルを変更できない


    以下のソースコードでは、UIButtonのタイトルは正しく設定できません。

    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(10.0f, 120.0f, 300.0f, 50.0f);
    button.titleLabel.text = @"ボタン名";

    UIButtonのタイトルはsetTitle:forState:を使って書き換えます。

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(10.0f, 120.0f, 300.0f, 50.0f);
    [button setTitle:@"ボタン名" forState:UIControlStateNormal];