繁体   English   中英

IOS 7中的自定义UITabbar问题

[英]Custom UITabbar issue in IOS 7

我自定义UITabbar的方法在IOS 5和IOS 6中运行良好,但在IOS7中Tabbar没有显示任何图像。

IOS6结果:

在此输入图像描述

IOS7结果:

在此输入图像描述

在做了一些研究后,我尝试修复现有的代码,但没有成功。我的代码在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

现在在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];

  }

以上代码在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]];

但结果仍然相同,任何帮助将不胜感激,谢谢。

我在https://stackoverflow.com/a/20007782/1755055查看了我的答案。

我相信在ios7中使用外观类属性存在限制或错误。

您的标签栏项目使用图标图像作为模板,并使用色调颜色对其进行着色。 Apple真正要求你做的是为大多数透明的标签栏设计图标,以便它们可以用作模板图像。

有关设计这些内容的讨论,请参阅MobileHIG文档中的条形按钮图标 (第204页)。

因此,要设置所选的标签栏项,您需要在'UITabBarItem'上调用'setSelectedImage:',您可以从UIViewContoller获取该项。 如果您的UIViewController的子类被选项卡上的NavigationController包装,您将从该ViewController获取选项卡栏项。

我使用故事板,所以我可以在Interface Builder中设置标签图像。 selectedImage属性现在不可用,因此您必须在代码中设置它。 我在每个主视图控制器中执行此操作,这些控制器出现在每个选项卡中导航控制器堆栈的顶部。

您的示例需要按照设计渲染图像,因此您还需要在图像上设置渲染模式。

- (void)viewDidLoad
{
    [super viewDidLoad];

    ...

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

}

它不是色调,但你可以使用图像:

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

您是否尝试过直接在标签栏的实例上设置barTintColor而不是UIAppearance代理?

这是iOS 7中的已知问题.tintColor用于选定的选项卡图像。 selectedImageTintColor完全被忽略。 没有办法着色未选择的标签图像。

有关此问题,请参阅Apple开发者论坛https://devforums.apple.com/message/851126#851126上的讨论。

暂无
暂无

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

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