#import "RootViewController.h"
#import "oneViewController.h"
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kMargin 20
#define kWidth 50
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"工具栏";
self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.920 blue:0.428 alpha:1.000];
//UIToolbar, 工具栏类, 继承与UIView
//在View上创建toolbar工具栏
//第一个
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, kMargin + 64, kScreenWidth, kWidth)];
toolBar.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.890];
toolBar.barStyle = UIBarStyleDefault;//底色系统色(白色)
// toolBar.barStyle = UIBarStyleBlack;//底色灰色
//toolBar按钮的颜色
//设定方式1:直接用.tintColor来设定,注:在这里设定只能改变item的颜色是默认颜色(蓝色)的item
//设定方式2:直接设ietm的颜色
toolBar.tintColor = [UIColor colorWithRed:0.183 green:0.985 blue:0.117 alpha:1.000];
//*****在toolBer上创建按钮的方式1:在item上直接创建好方数组中添加上去(使用系统的)
//创建一个可变数组用来接收barButtonitem按钮
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
//创建多个UIBarButtonItem按钮
// UIBarButtonItem, 继承于 UIBarItem
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
barButtonItem.tintColor = [UIColor redColor];//自定颜色
[array addObject:barButtonItem];//把item按钮添加到数组中
[barButtonItem release];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
[array addObject:barButtonItem1];//把item按钮添加到数组中
barButtonItem1.tintColor = [UIColor colorWithRed:0.851 green:0.115 blue:1.000 alpha:1.000];//修改单个按钮的颜色
[barButtonItem1 release];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil];
[array addObject:barButtonItem2];//把item按钮添加到数组中
[barButtonItem2 release];
UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
[array addObject:barButtonItem3];//把item按钮添加到数组中
[barButtonItem3 release];
UIBarButtonItem *barButtonItem4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
[array addObject:barButtonItem4];//把item按钮添加到数组中
[barButtonItem4 release];
//把toolBar的itme设为数组中的按钮
//第一种写法
//toolBar.items = array;
//第二种写法
[toolBar setItems:array animated:YES];
[self.view addSubview:toolBar];
[toolBar release];
//第二个
UIToolbar *toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(kMargin, kScreenHeight - 4 * kMargin, kScreenWidth - 2 * kMargin, kWidth)];
//*****在toolBar上创建按钮方式2:创建button按钮放到item上,在把item放到数组中添加上去
//创建一个button按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 50, 50);
//背景色
button.backgroundColor = [UIColor colorWithRed:0.080 green:0.179 blue:0.967 alpha:1.000];
[button setTitle:@"变色 " forState:UIControlStateNormal];
//button渲染色
button.tintColor = [UIColor whiteColor];
button.titleLabel.font = [UIFont boldSystemFontOfSize:23];
//给button设置圆角
button.layer.borderWidth = 4;
button.layer.borderColor = [UIColor colorWithWhite:0.000 alpha:0.110].CGColor;
button.layer.cornerRadius = 25;
[button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
//把button按钮添加barButtonItem上
UIBarButtonItem *barButtonItem9 = [[UIBarButtonItem alloc] initWithCustomView:button];
//barButtonItem9添加到toolBar上
toolBar1.items = @[barButtonItem9];
//设置背景图片
[toolBar1 setBackgroundImage:[UIImage imageNamed:@"11.jpg"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];
//设置图片阴影
[toolBar1 setShadowImage:[UIImage imageNamed:@"11.jpg"] forToolbarPosition:UIBarPositionBottom];
[self.view addSubview:toolBar1];
[toolBar1 release];
//*****在toolBar上创建按钮方式3:自定义UIBarButtonItem放上去
//第三个toolBar
UIToolbar *toolBar2 = [[UIToolbar alloc] initWithFrame:CGRectMake(kMargin, kScreenHeight - 400, kScreenWidth - 2 * kMargin, kWidth)];
toolBar2.tintColor = [UIColor blueColor];//为toolBar上所有的按钮设置颜色
UIBarButtonItem *item1 = [[[UIBarButtonItem alloc] initWithTitle:@"上一页" style:UIBarButtonItemStyleDone target:self action:@selector(pressButton1:)] autorelease];
item1.tintColor = [UIColor redColor];
UIBarButtonItem *item2 = [[[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStyleDone target:self action:nil] autorelease];
UIBarButtonItem *item3 = [[[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStyleDone target:self action:nil] autorelease];
//弹簧按钮
UIBarButtonItem * spaceItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];
toolBar2.items = @[item1, spaceItem, item3, spaceItem, item2];
//工具栏半透明度, 默认是YES,
toolBar2.translucent = NO;
[self.view addSubview:toolBar2];
}
//变色
- (void)pressButton:(UIButton *)button {
NSInteger number1 = arc4random() % 256;
NSInteger number2 = arc4random() % 256;
NSInteger number3 = arc4random() % 256;
self.view.backgroundColor = [UIColor colorWithRed:number1 / 255. green:number2 / 255. blue:number3 / 255. alpha:1.000];
}
//上一页
- (void)pressButton1:(UIButton *)button {
oneViewController *oneVC = [[oneViewController alloc] init];
[self.navigationController pushViewController:oneVC animated:YES];
[oneVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import "oneViewController.h"
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kMargin 20
#define kWidth 50
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"工具栏";
self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.920 blue:0.428 alpha:1.000];
//UIToolbar, 工具栏类, 继承与UIView
//在View上创建toolbar工具栏
//第一个
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, kMargin + 64, kScreenWidth, kWidth)];
toolBar.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.890];
toolBar.barStyle = UIBarStyleDefault;//底色系统色(白色)
// toolBar.barStyle = UIBarStyleBlack;//底色灰色
//toolBar按钮的颜色
//设定方式1:直接用.tintColor来设定,注:在这里设定只能改变item的颜色是默认颜色(蓝色)的item
//设定方式2:直接设ietm的颜色
toolBar.tintColor = [UIColor colorWithRed:0.183 green:0.985 blue:0.117 alpha:1.000];
//*****在toolBer上创建按钮的方式1:在item上直接创建好方数组中添加上去(使用系统的)
//创建一个可变数组用来接收barButtonitem按钮
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
//创建多个UIBarButtonItem按钮
// UIBarButtonItem, 继承于 UIBarItem
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
barButtonItem.tintColor = [UIColor redColor];//自定颜色
[array addObject:barButtonItem];//把item按钮添加到数组中
[barButtonItem release];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
[array addObject:barButtonItem1];//把item按钮添加到数组中
barButtonItem1.tintColor = [UIColor colorWithRed:0.851 green:0.115 blue:1.000 alpha:1.000];//修改单个按钮的颜色
[barButtonItem1 release];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil];
[array addObject:barButtonItem2];//把item按钮添加到数组中
[barButtonItem2 release];
UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
[array addObject:barButtonItem3];//把item按钮添加到数组中
[barButtonItem3 release];
UIBarButtonItem *barButtonItem4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
[array addObject:barButtonItem4];//把item按钮添加到数组中
[barButtonItem4 release];
//把toolBar的itme设为数组中的按钮
//第一种写法
//toolBar.items = array;
//第二种写法
[toolBar setItems:array animated:YES];
[self.view addSubview:toolBar];
[toolBar release];
//第二个
UIToolbar *toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(kMargin, kScreenHeight - 4 * kMargin, kScreenWidth - 2 * kMargin, kWidth)];
//*****在toolBar上创建按钮方式2:创建button按钮放到item上,在把item放到数组中添加上去
//创建一个button按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 50, 50);
//背景色
button.backgroundColor = [UIColor colorWithRed:0.080 green:0.179 blue:0.967 alpha:1.000];
[button setTitle:@"变色 " forState:UIControlStateNormal];
//button渲染色
button.tintColor = [UIColor whiteColor];
button.titleLabel.font = [UIFont boldSystemFontOfSize:23];
//给button设置圆角
button.layer.borderWidth = 4;
button.layer.borderColor = [UIColor colorWithWhite:0.000 alpha:0.110].CGColor;
button.layer.cornerRadius = 25;
[button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
//把button按钮添加barButtonItem上
UIBarButtonItem *barButtonItem9 = [[UIBarButtonItem alloc] initWithCustomView:button];
//barButtonItem9添加到toolBar上
toolBar1.items = @[barButtonItem9];
//设置背景图片
[toolBar1 setBackgroundImage:[UIImage imageNamed:@"11.jpg"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];
//设置图片阴影
[toolBar1 setShadowImage:[UIImage imageNamed:@"11.jpg"] forToolbarPosition:UIBarPositionBottom];
[self.view addSubview:toolBar1];
[toolBar1 release];
//*****在toolBar上创建按钮方式3:自定义UIBarButtonItem放上去
//第三个toolBar
UIToolbar *toolBar2 = [[UIToolbar alloc] initWithFrame:CGRectMake(kMargin, kScreenHeight - 400, kScreenWidth - 2 * kMargin, kWidth)];
toolBar2.tintColor = [UIColor blueColor];//为toolBar上所有的按钮设置颜色
UIBarButtonItem *item1 = [[[UIBarButtonItem alloc] initWithTitle:@"上一页" style:UIBarButtonItemStyleDone target:self action:@selector(pressButton1:)] autorelease];
item1.tintColor = [UIColor redColor];
UIBarButtonItem *item2 = [[[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStyleDone target:self action:nil] autorelease];
UIBarButtonItem *item3 = [[[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStyleDone target:self action:nil] autorelease];
//弹簧按钮
UIBarButtonItem * spaceItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];
toolBar2.items = @[item1, spaceItem, item3, spaceItem, item2];
//工具栏半透明度, 默认是YES,
toolBar2.translucent = NO;
[self.view addSubview:toolBar2];
}
//变色
- (void)pressButton:(UIButton *)button {
NSInteger number1 = arc4random() % 256;
NSInteger number2 = arc4random() % 256;
NSInteger number3 = arc4random() % 256;
self.view.backgroundColor = [UIColor colorWithRed:number1 / 255. green:number2 / 255. blue:number3 / 255. alpha:1.000];
}
//上一页
- (void)pressButton1:(UIButton *)button {
oneViewController *oneVC = [[oneViewController alloc] init];
[self.navigationController pushViewController:oneVC animated:YES];
[oneVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
在这里我们需要创建一个视图(oneViewController)用于跳转,我就不创建了,你自己写一个就好