【问题标题】:UITabBarItem Image is in Blue and not changedUITabBarItem 图像为蓝色且未更改
【发布时间】:2013-12-03 04:45:39
【问题描述】:

嗯,使用我自己的TabBarController,即ElWafyatTabBarController,基本上它继承自UITabBarController

ElWafyatTabBarController.m -> viewDidLoad

我创建了一些ViewControllers,然后我使用rootViewController 创建了UINavigationController 到我在上一步中创建的viewControllers

然后我创建了UITabBarItem,并为每个具有rootViewController 的导航控制器设置了图像和标题到我在第一步中创建的viewControllers,并为这些@987654332 设置tabBarItem @给这些tabBarItem

我发现的问题是 UITabBarItem 的标题显示正确,但图像显示为蓝色。

仅供参考:我创建了两个名为 test.png 和 test@2x.png 的图像,尺寸分别为 32x32 和 64x64 我仍然有这些问题, 那么任何人都可以提供帮助吗?

这是我的代码:

ElWafyatTabBarController -> viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    HomeViewController *homeViewController = [[HomeViewController alloc]
        initWithNibName:@"HomeViewController" bundle:nil];
    NaaiViewController *naaiViewController = [[NaaiViewController alloc]
        initWithNibName:@"NaaiViewController" bundle:nil];
    MushatraViewController *mushatraViewController = [[MushatraViewController alloc]
        initWithNibName:@"MushatraViewController" bundle:nil];
    TakremViewController *takremViewController = [[TakremViewController alloc]
        initWithNibName:@"TakremViewController" bundle:nil]
    UINavigationController *homeNavC = [[UINavigationController alloc]initWithRootViewController:homeViewController];
    UINavigationController *naaiNavC = [[UINavigationController alloc]initWithRootViewController:naaiViewController];
    UINavigationController *mushatraNavC = [[UINavigationController alloc]initWithRootViewController:mushatraViewController];
    UINavigationController *takremNavC = [[UINavigationController alloc]initWithRootViewController:takremViewController];

    // Setup Controllers for Tab Bar. (first level).
//    [homeNavC.tabBarItem setTitle:@"الرئيسية"];
//    [naaiNavC.tabBarItem setTitle:@"نعي"];
//    [mushatraNavC.tabBarItem setTitle:@"مشاطرة"];
//    [takremNavC.tabBarItem setTitle:@"تكريم"];

    homeNavC.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
    naaiNavC.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
    mushatraNavC.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
    takremNavC.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];

    homeNavC.navigationBar.barStyle = UIBarStyleBlack;
    naaiNavC.navigationBar.barStyle = UIBarStyleBlack;
    mushatraNavC.navigationBar.barStyle = UIBarStyleBlack;
    takremNavC.navigationBar.barStyle = UIBarStyleBlack;

    UITabBarItem* tabBarItem =  [[UITabBarItem alloc] initWithTitle:@"Colors" image:[UIImage imageNamed:@"test.png"] tag:9];

    homeNavC.tabBarItem = tabBarItem;

    myViewControllers = [ NSArray arrayWithObjects:takremNavC, mushatraNavC, naaiNavC, homeNavC,nil];
    [self setViewControllers:myViewControllers animated:YES];
    [self.tabBarController setSelectedIndex:3];
    [self setSelectedIndex:3];
} 

这些输出:

【问题讨论】:

    标签: ios objective-c xcode uitabbar uitabbaritem


    【解决方案1】:

    如果要查看自己的图片,需要将图片的渲染模式设置为UIImageRenderingModeAlwaysOriginal,否则图片显示为模板图片。你应该阅读标签栏上的文档,它有这样的声明:

    标签栏项目图标

    标签栏中的每个项目都可以有一个自定义的选定图像和未选定图像 图片。您可以在初始化选项卡栏项目时指定这些图像 使用 initWithTitle:image:selectedImage: 方法。请注意,一个选项卡 栏项图像将自动呈现为模板图像 在选项卡栏中,除非您明确将其呈现模式设置为 UIImageRenderingModeAlwaysOriginal。有关详细信息,请参阅模板 图片。

    【讨论】:

    • 别忘了 UIImageRenderingModeAlwaysOriginal 仅适用于 iOS 7.0 及更高版本
    • 如何/在哪里设置 UIImageRenderingModeAlwaysOriginal?
    • image.withRenderingMode(.alwaysOriginal)
    【解决方案2】:

    转到Assets.xcassets > Click on you image > Attribute Inspector > Render As > Original Image

    【讨论】:

      【解决方案3】:

      蓝色是iOS 7中UITabbar的默认色调。您可以通过设置将其更改为您喜欢的任何颜色:

      myTabbar.tintColor = [UIColor greenColor]; //Put the color of your choice
      

      【讨论】:

        【解决方案4】:

        如果你有 UITabBar:

         UITabBar *tabBar = _tabBarController.tabBar;
        
         UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
        

        像这样设置tabBarItem1的图像:

         [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"Selected_your_image"] withFinishedUnselectedImage:[UIImage imageNamed:@"UNSelected_your_image"]];
        

        为 UITabBar 的每个 UITabBarItem(s) 设置选中和未选中的图像。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-20
          • 1970-01-01
          • 2019-02-07
          • 1970-01-01
          • 1970-01-01
          • 2017-08-19
          相关资源
          最近更新 更多