//
// RootViewController.m
// UIToolBar
//
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// 设置title
self.title = @"UIToolBar的使用";
// 设置背景色
self.view.backgroundColor = [UIColor blueColor];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// 添加一个UIToolBar
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 2*44, self.view.frame.size.width, 44)];
toolBar.tintColor = [UIColor purpleColor]; // 设置颜色
toolBar.barStyle = UIBarStyleBlackTranslucent; // 设置样式
toolBar.translucent = YES; // 设置为半透明的,但样式必须设置为UIBarStyleBlackTranslucent
toolBar.hidden = NO; // 隐藏toolBar
// [toolBar setBackgroundImage:[UIImage imageNamed:@"abc"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; //设置背景图片
// [toolBar setShadowImage:[UIImage imageNamed:@"abc"] forToolbarPosition:UIToolbarPositionBottom];
// 添加到view上
[self.view addSubview:toolBar];
[toolBar release];
// 自定义样式
UIBarButtonItem *firstButton = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleBordered target:selfaction:nil];
firstButton.tintColor = [UIColor redColor];
// 设置固定的空隙
UIBarButtonItem *twoButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:selfaction:nil];
twoButton.width = 30.0f;
twoButton.tintColor = [UIColor greenColor];
// 播放样式按钮
UIBarButtonItem *threeButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:selfaction:nil];
threeButton.tintColor = [UIColor whiteColor];
// 几个空间之间的距离平均分配
UIBarButtonItem *twoButton1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:selfaction:nil];
twoButton1.tintColor = [UIColor greenColor];
// 暂停样式按钮
UIBarButtonItem *fourButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:selfaction:nil];
fourButton.tintColor = [UIColor yellowColor];
// 添加到UIToolBar上面,并释放内存
[toolBar setItems:@[firstButton, twoButton, threeButton, twoButton1, fourButton] animated:YES];
[firstButton release], firstButton = nil;
[twoButton release], twoButton = nil;
[threeButton release], threeButton = nil;
[twoButton1 release], twoButton1 = nil;
[fourButton release], fourButton = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end