//
// ViewController.m
// UIToolbarAll
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
//UIToolbar 就是代表工具条 它通常仅作为多个UIBarButtonItem的容器
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 46)];
// 设置工具条的style
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
/*
barStyle:
UIBarStyleDefault(默认风格)
UIBarStyleBlack(黑色背景、白字风格)
UIBarStyleBlackOpaque(黑色不透明背景、白字风格)
UIBarStyleBlackTranslucent(黑色透明背景、白字风格)
*/
// – setBackgroundImage:forToolbarPosition:barMetrics: //背景图片
// - setShadowImage:forToolbarPosition://阴影图片
//创建UIBarButtonItem
/*
系统图标
initWithBarButtonSystemItem:target:action:
图片
initWithImage:style:target:action:
文字
initWithTitle:style:target:action:
自定义视图
initWithCustomView:
*/
/*
另外还要提到两种特殊的item,作为分隔符使用的
UIBarButtonSystemItemFlexibleSpace //会自动调整宽度
UIBarButtonSystemItemFixedSpace //固定宽度
*/
// 创建使用系统图标的UIBarButtonItem
UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem * fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItem.width = 20;
UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithTitle:@"文字" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem * flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(onClick:)];
[rightItem setTag:1];
toolbar.items = @[item1,fixedItem,item2,flexItem,rightItem];
// 将工具条添加到当前应用的界面中
[self.view addSubview:toolbar];
}
- (void)onClick:(id) sender {
NSString* title = @"";
switch ([sender tag]) {
case 1:
title = @"文字";
break;
default:
break;
}
NSLog(@"%@", title);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end