UITabBar和UIToolbar是不同的,必定要分清楚!!!ide
UINavigationController 里UINavigationBar 和 UIToolbar 是一套的.
当UIViewController视图self.view加载到KeyWindow屏幕上时navigationbar 和 toolbar也都被添加到了keywindow上,而默认状况下,toolbar 是隐藏的,是一个很灵活的工具栏,能够在须要时显示出来UITabbarcontroller里的子视图UITabbar和UIToolbar是不同的,UITabbar通常看成多个根视图之间的切换工具,用来嵌套视图控制器,灵活度不高.工具
通常状况下,tabbar就是切换不一样的view,toolbar就是view里的快捷按钮差很少spa
UITabBarController的底部有一个tabbar,也就是UITabbar对象,用户控制器之间快速切换。orm
UINavigationController控制器的底部有一个toolbar(默认是隐藏的,可用经过[self.navigationControllersetToolbarHidden:NO])显示出来,UIToolBar对象,能够在上面添加UIBarButtonItem当作快捷键使用。对象
这是咱们最多见用到UITabbar和UIToolBar的地方,可是其实这两个对象仍是很好用的,好比你须要定义工具条,是能够经过建立view,在view上添加按钮,设置按钮的位置,为什么不用UIToolBar呢,经过UIToolBar的setItems把UIBarButtonItem添加到UIToolBar里面,他们的位置会自动计算好省去了繁琐的位置计算。rem
说道这里说一下UITabbar和UIToolBar的区别:get
UITabbar和UIToolBar在不少地方是能够相互替换使用的,他们的区别是:string
UITabbar上的item,选中时是高亮的,未选中则是普通状态,而UIToolBar这时没用高亮的状态。it
常见的建立toolBar的方法以下:io
- (void)setToolBar{
UIToolbar *toolBar = [[UIToolbar alloc] init];
NSMutableArray *items = [NSMutableArray array];
NSArray *itemsettings = @[@{@"imageName":@"compose_toolbar_picture",@"actionName":@"selectPicture"},@{@"imageName":@"compose_mentionbutton_background"},@{@"imageName":@"compose_trendbutton_background"},@{@"imageName":@"compose_emoticonbutton_background",@"actionName":@"selectEmoticonKeyboard"},@{@"imageName":@"compose_add_background"}];
for (NSDictionary *itemDict in itemsettings) {
UIButton *btn = [[UIButton alloc] init];
[btn setBackgroundImage:[UIImage imageNamed:itemDict[@"imageName"]] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted",itemDict[@"imageName"]]] forState:UIControlStateHighlighted];
[btn sizeToFit];
NSString *actionStr = itemDict[@"actionName"];
if ([actionStr isEqualToString:@"selectPicture"]) {
[btn addTarget:self action:@selector(selectPicture) forControlEvents:UIControlEventTouchUpInside];
}else if([actionStr isEqualToString:@"selectEmoticonKeyboard"]){
[btn addTarget:self action:@selector(selectEmoticonKeyboard) forControlEvents:UIControlEventTouchUpInside];
}
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
[items addObject:barItem];
//添加弹簧 等间距
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spaceItem];
}
[items removeLastObject];
toolBar.items = items;
[self.view addSubview:toolBar];
[toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self.view);
make.height.mas_equalTo(44);
}];
}