建立一个UIToolbar数组
UIToolbar能够手动建立,也能够打开系统自带的ide
//手动建立一个UIToolbar,设置它的位置和大小 UIToolbar *myToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 100)]; //设置UIToolbar的颜色 myToolbar.barTintColor = [UIColor redColor]; //将UIToolbar添加到手机界面上 [self.view addSubview:myToolbar];
给UIToolbar上添加spa
UIBarButtonItemcode
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //手动建立一个UIToolbar,设置它的位置和大小 UIToolbar *myToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 100)]; //设置UIToolbar的颜色 myToolbar.barTintColor = [UIColor redColor]; //将UIToolbar添加到手机界面上 [self.view addSubview:myToolbar]; //打开系统自带的UIToolbar [self.navigationController setToolbarHidden:NO animated:YES]; //给UIToolbar添加Button //方法1)给UIToolbar上添加一个能够点击的文字UIBarButtonItem,点击可有方法,能够触发事件 UIBarButtonItem *barOne = [[UIBarButtonItem alloc]initWithTitle:@"首页" style:UIBarButtonItemStylePlain target:self action:@selector(haha:)]; //方法2)给UIToolbar添加一个图片Button,是镂空透明图,颜色能够设置 UIBarButtonItem *barTwo = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"1"] style:UIBarButtonItemStylePlain target:self action:@selector(haha1:)]; //方法3) //手动添加一个Button UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 30)]; btn.backgroundColor = [UIColor blackColor]; [btn addTarget:self action:@selector(haha2:) forControlEvents:UIControlEventTouchUpInside]; //将手动建立的Button赋给UIToolbar UIBarButtonItem *barThree = [[UIBarButtonItem alloc]initWithCustomView:btn]; //这个UIBarButtonItem是可扩展的,可拉伸的(调整间距用) UIBarButtonItem *fileButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; //调整全部的UIBarButtonItem(镂空透明图案)颜色 [self.navigationController.toolbar setTintColor:[UIColor purpleColor]]; //要用数组给UIToolbar添加按钮 NSArray *arrs = @[barOne,fileButton,barTwo,fileButton,barThree]; //将数组赋给本身新建的UIToolbar myToolbar.items=arrs; //将数组赋给系统自带的UIToolbar [self setToolbarItems:arrs animated:YES]; }