UIToolbar的简单用法
UIToolbar
开发中经常会用到的控件之一,实现起来也很简单,与此同时我们还要知道 UIBarButtonItem 和 Fixed Space Bar Button Item,这两个小东西是在Bar上的按钮和间距,都被对象化了。
来看代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#import "ViewController.h"@interface ViewController ()//声明@property (nonatomic, strong) UIToolbar *mytoolbar;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //实例化 self.mytoolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)]; //添加到视图 [self.view addSubview:self.mytoolbar]; //选择风格,这里我们选择黑色风格 self.mytoolbar.barStyle = UIBarStyleBlack; //添加按钮和按钮之间的间距,这些都被对象化了,按钮是可以实现方法的 UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@"hello" style:UIBarButtonItemStylePlain target:self action:@selector(sayhello)]; UIBarButtonItem *fixed = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"bye" style:UIBarButtonItemStylePlain target:self action:@selector(saybye)]; //实例化的UIToolbar里面有items属性,是一个数组,用来存放我们要加上去的按钮 self.mytoolbar.items = @[item1, fixed, item2];}//点击item要实现的方法,输出hello或者bye- (IBAction)sayhello{ NSLog(@"hello");}- (IBAction)saybye{ NSLog(@"bye");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end |
浙公网安备 33010602011771号