Skip to content

Instantly share code, notes, and snippets.

@myaaaaa-chan
Created May 1, 2013 07:07
Show Gist options
  • Save myaaaaa-chan/5494112 to your computer and use it in GitHub Desktop.
Save myaaaaa-chan/5494112 to your computer and use it in GitHub Desktop.
UIBarButtonItemのイメージを変更する ref: http://qiita.com/items/9753c0c8934212b3f282
// 変更後のイメージ
UIImage *img = [UIImage imageNamed:@"hoge"];
// 変更後のイメージを設定するボタン
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// イメージを設定
[button setImage:img forState:UIControlStateNormal];
// ボタンにターゲットを設定
[button addTarget:self action:@selector(onButtonClicked:) forControlEvents:(UIControlEventTouchUpInside)];
button.frame = CGRectMake(0, 0, img.size.width, img.size.height);
// 変更後のBarButtonを用意
UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:button];
// styleを変更(変更できない・・・)
[barButton setStyle:UIBarButtonItemStyleBordered];
barButton.style = UIBarButtonItemStyleDone;
// 既存のtoolbarのアイテムを取得
NSArray *barButtonItemArray = [self.myToolBar items];
NSMutableArray *changedBarButtonItemArray = [NSMutableArray arrayWithArray:barButtonItemArray];
// イメージを変更したいアイテムを入れ替える
[changedBarButtonItemArray replaceObjectAtIndex:4 withObject:barButton];
// 変更後のアイテムの一覧をtoolbarに設定する
self.myToolBar.items = changedBarButtonItemArray;
[barButton setStyle:UIBarButtonItemStyleBordered];
barButton.style = UIBarButtonItemStyleDone;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment