建立一個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]; }