UIToolbarにカスタム画像のボタンを設置する方法
UIToolbarにカスタム画像のボタンを設置する方法について考えました。
以下のようにしてしまうと、UIToolBarに置いたカスタムボタンが青抜きの変なボタンになってしまうという問題が発生します。
UIBarButtonItem *ok = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"buttonOK.fw.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(ok)];
こちらのブログを参考にさせていただき、以下のコードを書いたところ解決です!
// とりあえず普通にカスタム画像のボタンを作っちゃう
UIButton *buttonOK = [UIButton buttonWithType:UIButtonTypeCustom]; buttonOK.frame = CGRectMake(0, 0, 61, 30);
[buttonOK setImage:[UIImage imageNamed:@"buttonOK.fw.png"] forState:UIControlStateNormal];
[buttonOK addTarget:self action:@selector(ok) forControlEvents:UIControlEventTouchUpInside];
// UIToolbarのUIBarButtonItemに今作ったカスタム画像のボタンを乗せる
UIBarButtonItem *ok = [[UIBarButtonItem alloc] initWithCustomView:buttonOK];
あとは、別途、action:@selector(ok) 部分のメソッドを作ればOK。同じような要領で、もうひとつキャンセルボタンを作ります。ただ、そのままだとボタン同士が接近するので、以下のコードでスペースを自動で入れてもらいます。
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
こんな感じで、カスタムボタンをUIToolbarに入れることができました!!