// 巻き戻りボタン生成
UIBarButtonItem *rewindItem
= [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
target:self
action:@selector(doRewind)]
autorelease];
// 再生ボタン生成
UIBarButtonItem *playItem
= [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:@selector(doPlay)]
autorelease];
// 早送りボタン生成
UIBarButtonItem *forwardItem
= [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
target:self
action:@selector(doForward)]
autorelease];
// 可変長スペース生成
UIBarButtonItem *flexibleItem
= [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil]
autorelease];
// 固定長スペース生成
UIBarButtonItem *fixedItem
= [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil]
autorelease];
fixedItem.width = 100;
// ツールバーにボタンを配置
self.toolbarItems = [NSArray arrayWithObjects:flexibleItem, rewindItem, fixedItem, playItem, fixedItem, forwardItem, flexibleItem, nil];
上のようなかんじでUIBarButtonItemインスタンスを生成して、NSArrayに格納しUIToolbarのitemsに渡します。ツールバーの並びはNSArrayに格納した順番になります。
また、ボタンだけでなく可変長・固定長のスペースも同様に配置します。
これを実行すると以下のように表示されます。
また上のサンプルコードでは initWithBarButtonSystemItem を利用してボタンを生成しましたがこれは iOS が持っているUIBarButtonSystemItem を指定するメソッドです。
指定した文字列や画像を指定する場合
- -initWithTitle: style: target: action:
- 文字列のボタンを生成する
- -initWithImage: style: target: action:
- 指定した画像のボタンを生成する

0 件のコメント:
コメントを投稿