今日はtoolbarに3セクションのUISegementedControlを加える方法です。
早い話がこれのことです。
xcodeのstoryboardでUISegementedControlをtoolbarに持っていくと、勝手に黒くちいさくしてくれるのですが、セグメントの数は変えられません。
なので、プログラムで書いていくことになります。
そして、こちらがソースコードです。
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.barStyle = UIBarStyleBlackOpaque;
UISegmentedControl *segs = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"一つ目", @"二つ目", @"3つ目", nil]];
[segs setSelectedSegmentIndex:0];
[segs setSegmentedControlStyle:UISegmentedControlStyleBar];
[segs addTarget:self action:@selector(optionChanged:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]initWithCustomView:segs];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:space, buttonItem, space, nil]];
[self.view addSubview:toolbar];
簡単ですね♪
こちらをコピペしました→http://www.18th-technote.com/post/9718092082/uitoolbar

No comments:
Post a Comment