自定义 TabBar ios 7-ios 8 按钮和背景

Posted

技术标签:

【中文标题】自定义 TabBar ios 7-ios 8 按钮和背景【英文标题】:customizing a UITabBar ios7-ios8 buttons and background 【发布时间】:2014-12-11 17:25:49 【问题描述】:

我一直在努力更改 uitabbar 图标和背景,以便它看起来像我想要的那样。每次我更改背景颜色时,它都会显示颜色,但不是纯色作为我给出的特定颜色。我为项目设置的图像在运行时也会变灰。

所以我有一个开始按钮的简历。 按钮代码:

- (IBAction)start:(id)sender 

UIStoryboard *storyBoard = [self storyboard];
Start *startViewController = [storyBoard instantiateViewControllerWithIdentifier:@"start"];
[self presentViewController:startViewController animated:NO completion:nil];

如您所见,它只发送到第二个 VC,即 UITabBarController。 在我尝试过的 appdelegate 中:

    Start *tabController = (Start *)self.window.rootViewController;

//tab1
UITabBar *tabBar = (UITabBar *)tabController.tabBar;
UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
[item1 setImage:[[UIImage imageNamed:@"1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

//tab2
UITabBarItem *item2 = [tabBar.items objectAtIndex:1];
[item2 setImage:[[UIImage imageNamed:@"2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
//tab3
UITabBarItem *item3 = [tabBar.items objectAtIndex:2];
[item3 setImage:[[UIImage imageNamed:@"3.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
//tab4
UITabBarItem *item4 = [tabBar.items objectAtIndex:3];
[item4 setImage:[[UIImage imageNamed:@"4.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

但它似乎没有工作。我看了很多,但我能找到的只是关于 setFinishedSelectedImage: withFinishedUnselectedImage:; 的教程这并没有真正帮助,因为它已被弃用。到目前为止,我所能做的就是从 interfacebuilder 设置 uitabbar。

要更改颜色,我只是更改了色调和背景颜色。还尝试在 uitabbar 后面放置一个 uiimageview,但它似乎也不起作用。

【问题讨论】:

【参考方案1】:

使用这个代码来定制一个UITabBar ios7-ios8

 UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
    UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];
    UITabBarItem *item3 = [self.tabBar.items objectAtIndex:3];

    item0.title = @"item0title";
    item1.title = @"item1title";
    item2.title=@"item2title";
    item3.title=@"item3title";


    [item0 setImage:[[UIImage imageNamed:@"imagename"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item0 setSelectedImage:[[UIImage imageNamed:@"imagename"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item1 setImage:[[UIImage imageNamed:@"imagename"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item1 setSelectedImage:[[UIImage imageNamed:@"imagename"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item2 setImage:[[UIImage imageNamed:@"imagename"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item2 setSelectedImage:[[UIImage imageNamed:@"imagename"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    [item3 setImage:[[UIImage imageNamed:@"imagename"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item3 setSelectedImage:[[UIImage imageNamed:@"imagename"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    //[[UITabBar appearance] setSelectedImageTintColor:[uicolor whitcolor]];
    //[[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];
    //[[UITabBar appearance] setTintColor:[UIColor yellowColor]];
    [[UITabBarItem appearance] setTitleTextAttributes:@ NSForegroundColorAttributeName : [UIColor yellowColor] 
                                             forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:@ NSForegroundColorAttributeName : [UIColor whiteColor] 
                                             forState:UIControlStateNormal];

【讨论】:

感谢您的快速回复!抱歉这个菜鸟问题,但我应该在哪里使用它?我不确定如何从我的 appdelegate 引用到 tabBar,我试图创建一个 UITabBarController 没有任何成功 创建 uitabarcontroller 并在您的故事板中选择您的 tabarview 并添加您的自定义类

以上是关于自定义 TabBar ios 7-ios 8 按钮和背景的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]

ios开发 如何修改正确tabBar的高度?

自定义tabBar

ios定制中间突出的tabBar

自定义 Tabbar 和 Tabbar Button 项

隐藏 TabBar 并在按钮单击时显示 NavigationController 工具栏