简体   繁体   English

IOS 7中的自定义UITabbar问题

[英]Custom UITabbar issue in IOS 7

My way for customizing the UITabbar was working fine in IOS 5 and IOS 6,but in IOS7 the Tabbar did not show any image. 我自定义UITabbar的方法在IOS 5和IOS 6中运行良好,但在IOS7中Tabbar没有显示任何图像。

IOS6 Result: IOS6结果:

在此输入图像描述

IOS7 Result: IOS7结果:

在此输入图像描述

After doing some research i try to fix the existing code,but did not succeed.here is my code which was working fine in ios6 在做了一些研究后,我尝试修复现有的代码,但没有成功。我的代码在ios6中工作正常

#import <Foundation/Foundation.h>


@interface CustomTabBarItem : UITabBarItem  
{
UIImage *selectedImg;
UIImage *unSelectedImg;
}

@property (nonatomic, retain) UIImage *selectedImg;
@property (nonatomic, retain) UIImage *unSelectedImg;

@end


#import "CustomTabBarItem.h"


@implementation CustomTabBarItem

@synthesize selectedImg;
@synthesize unSelectedImg;



-(UIImage *) selectedImage
 {
   return self.selectedImg;
}

-(UIImage *) unselectedImage
{
    return self.unSelectedImg;
}

@end

Now in appDelegate 现在在appDelegate

self.tabBarController.delegate = self;

self.tabBarController.tabBar.frame = CGRectMake(0, self.tabBarController.tabBar.frame.origin.y, self.tabBarController.tabBar.frame.size.width, 49);

for(int i=1;i<=4;i++)
  {
    CustomTabBarItem *tabItem = [[CustomTabBarItem alloc] initWithTitle:@"" image:nil tag:0];
        tabItem.selectedImg=[UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_over_%@.png",i,deviceType]];
    tabItem.unSelectedImg=[UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_%@.png",i,deviceType]];

            UIEdgeInsets titleInsets = UIEdgeInsetsMake(6.0, 0.0, -6.0, 0.0);
    tabItem.imageInsets = titleInsets;
    [[self.tabBarController.viewControllers objectAtIndex:i-1] setTabBarItem:tabItem];
    [tabItem release];

  }

The above code is working fine in in IOS6,after doing some research i did some changes for IOS7 以上代码在IOS6中运行良好,经过一些研究后我对IOS7进行了一些更改

 [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];

CustomTabBarItem *tabItem = [[CustomTabBarItem alloc] initWithTitle:@"" image:nil tag:0];

tabItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_over_%@.png",i,deviceType]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];


   tabItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_%@.png",i,deviceType]];

but still the result is same , any help will be appreciated thanks. 但结果仍然相同,任何帮助将不胜感激,谢谢。

See my answer at https://stackoverflow.com/a/20007782/1755055 for what I worked out. 我在https://stackoverflow.com/a/20007782/1755055查看了我的答案。

I believe there is either a limitation or a bug in using the appearance class property for this in ios7. 我相信在ios7中使用外观类属性存在限制或错误。

Your tab bar item is using the icon image as a template and coloring it with the tint color. 您的标签栏项目使用图标图像作为模板,并使用色调颜色对其进行着色。 What Apple really wants you to do is design Icons for tab bars that are mostly transparent so that they can be used as a template image. Apple真正要求你做的是为大多数透明的标签栏设计图标,以便它们可以用作模板图像。

See Bar Button Icons in the MobileHIG document around page 204 for a discussion on designing these. 有关设计这些内容的讨论,请参阅MobileHIG文档中的条形按钮图标 (第204页)。

So to set the selected tab bar item you need to call 'setSelectedImage:' on the 'UITabBarItem' that you can get from UIViewContoller. 因此,要设置所选的标签栏项,您需要在'UITabBarItem'上调用'setSelectedImage:',您可以从UIViewContoller获取该项。 If your subclass of UIViewController is wrapped by a NavigationController on the tab, you get the tab bar item from that ViewController. 如果您的UIViewController的子类被选项卡上的NavigationController包装,您将从该ViewController获取选项卡栏项。

I use storyboards, so I can set the tab image in Interface Builder. 我使用故事板,所以我可以在Interface Builder中设置标签图像。 The selectedImage property is not available there right now, so you have to set it in code. selectedImage属性现在不可用,因此您必须在代码中设置它。 I did this in each of my main view controllers that appear at the top of navigation controller stacks in each tab. 我在每个主视图控制器中执行此操作,这些控制器出现在每个选项卡中导航控制器堆栈的顶部。

Your example needs to have the image rendered as it was designed, so you also need to set the rendering mode on the image. 您的示例需要按照设计渲染图像,因此您还需要在图像上设置渲染模式。

- (void)viewDidLoad
{
    [super viewDidLoad];

    ...

    [self.navigationController.tabBarItem setSelectedImage:[[UIImage imageNamed:@"MySelectedIcon.png"]
         imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];

}

it's not tint, but you can do it with images: 它不是色调,但你可以使用图像:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]];

Have you tried setting barTintColor on the instance of the tab bar directly, instead of the UIAppearance proxy? 您是否尝试过直接在标签栏的实例上设置barTintColor而不是UIAppearance代理?

This is a known issue in iOS 7. The tintColor is used for the selected tab image. 这是iOS 7中的已知问题.tintColor用于选定的选项卡图像。 The selectedImageTintColor is completely ignored. selectedImageTintColor完全被忽略。 There is no way to tint unselected tab images. 没有办法着色未选择的标签图像。

See a discussion on the Apple Developer Forums https://devforums.apple.com/message/851126#851126 about this. 有关此问题,请参阅Apple开发者论坛https://devforums.apple.com/message/851126#851126上的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM