你是否曾经想要创建一个不需要 UITabBarController 的 UITabBar 类似控制?如果是,那么这是一个解决方案。有时,设计师在没有真正理解应用程序需要标签架构的情况下绘制 UITabBar。我就是这样创建这个控制的。它是一个基于 UIView 的 Toolbar,看起来像 UITabBar,但不需要使用 UITabBarController。它支持在 iPhone 和 iPad 上垂直和水平方向上使用。它会自动扩展到其父 UIView 的宽度。此外,它还具有经典 UITabBar 中不存在的一些功能,例如禁用某些项以及动态更改按钮数量。它是一个 UIView 的子类,因此您可以按您想要的方式对其操作。
它支持 iOS 6.0 及更高版本。
有一个针对 iPhone 和 iPad 的通用应用示例项目。
最佳方式是通过 Cocoapods 添加源代码
pod 'DKToolbar'
示例代码
- (void)setupToolbar
{
// Create and add a toolbar as a subview. It calculates all frames automatically.
_toolbar = [[DKToolbar alloc] initInView:self.view withDelegate:self];
_toolbar.itemBackgroundColor = COLOR_CUSTOM_LIGHT_BLUE;
[self.view addSubview:_toolbar];
// Create and add items to the toolbar
DKToolbarItem *toolbarItem0 = [[DKToolbarItem alloc] initWithTitle:@"Response" image:[UIImage imageNamed:@"VacancyToolbarIconResponse"] selectedImage:nil];
DKToolbarItem *toolbarItem1 = [[DKToolbarItem alloc] initWithTitle:@"No resume" image:[UIImage imageNamed:@"VacancyToolbarIconNoResumeResponse"] selectedImage:nil];
DKToolbarItem *toolbarItem2 = [[DKToolbarItem alloc] initWithTitle:@"Call" image:[UIImage imageNamed:@"VacancyToolbarIconPhoneCall"] selectedImage:nil];
DKToolbarItem *toolbarItem3 = [[DKToolbarItem alloc] initWithTitle:@"To contacts" image:[UIImage imageNamed:@"VacancyToolbarIconAddContact"] selectedImage:nil];
_toolbar.items = @[toolbarItem0, toolbarItem1, toolbarItem2, toolbarItem3];
}
// DKToolbarDelegate
// Here you can handle clicks on tabs
- (void)toolbarItemClickedAtIndex:(NSInteger)index
{
NSLog(@"Toolbar item clicked at index:%d", (int)index);
NSString *message = nil;
switch (index) {
case ToolbarItemResponse:
{
message = @"ToolbarItemResponse";
break;
}
case ToolbarItemNoResumeResponse:
{
message = @"ToolbarItemResponse";
break;
}
case ToolbarItemMakeCall:
{
message = @"ToolbarItemMakeCall";
break;
}
case ToolbarItemAddContact:
{
message = @"ToolbarItemAddContact";
break;
}
default:
break;
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Item clicked" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
}