创建一个UIToolbar
UIToolbar可以手动创建,也可以打开系统自带的
//手动创建一个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上添加
UIBarButtonItem
#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];
}