与Instagram应用程序一样,我希望我的中心标签栏项目具有特定颜色。
我理解这是我如何设置所选图像:
[[UITabBar appearance] setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor colorWithRed:0.484 green:0.403 blue:0.884 alpha:1] forSize:CGSizeMake(64, 49) withCornerRadius:0]];
但是我如何保持第3个标签的背景始终保持这种颜色?
另外,如何将中间标签与其余标签分开,例如,当我按下中央标签时,有视图控制器的模态显示(如instagram),并且在解除模态视图控制器。
答案 0 :(得分:0)
@interface BaseViewController : UITabBarController
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image;
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage: (UIImage*)highlightImage;
@implementation BaseViewController
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
UIViewController* viewController = [[UIViewController alloc] init] ;
viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
return viewController;
}
//创建自定义UIButton并将其添加到标签栏的中心
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
}
对此Tab控制器进行子类化并调用上面的函数来设置图像o标签栏项目和Instagram等中心按钮
@interface InstagramViewController : BaseViewController
@end
@implementation InstagramViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.viewControllers = [NSArray arrayWithObjects:
[self viewControllerWithTabTitle:@"Feed" image:[UIImage imageNamed:@"112-group.png"]],
[self viewControllerWithTabTitle:@"Popular" image:[UIImage imageNamed:@"29-heart.png"]],
[self viewControllerWithTabTitle:@"Share" image:nil],
[self viewControllerWithTabTitle:@"News" image:[UIImage imageNamed:@"news.png"]],
[self viewControllerWithTabTitle:@"@user" image:[UIImage imageNamed:@"123-id-card.png"]], nil];
}
-(void)willAppearIn:(UINavigationController *)navigationController
{
[self addCenterButtonWithImage:[UIImage imageNamed:@"cameraTabBarItem.png"] highlightImage:nil];
}
@end