繁体   English   中英

自定义UITabBar背景图像在iOS 5及更高版本中不起作用

[英]Custom UITabBar background image not working in iOS 5 and later

我有一段简单的代码将背景图像放置在tabBar上。

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBG.png"]];
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
[imageView release];

在iOS 4中可以正常工作,但在iOS 5中进行测试时则无法正常工作。

我正在尝试执行以下操作:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBG.png"]];

NSString *reqSysVer = @"4.3";
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];

if ([iOSVersion compare:reqSysVer options:NSNumericSearch] !=NSOrderedDescending) {
    // code for iOS 4.3 or below
    [self.tabBarController.tabBar insertSubView:imageView atIndex:0];
}
else {
    // code for iOS 5
    [self.tabBarController.tabBar insertSubView:imageView atIndex:1];
}

[imageView release];

las,这行不通...任何人都可以提供解决方案?

iOS5提供了UIAppearance代理。

另外,最佳实践是基于功能(在本例中为responsToSelector)而不是iOS版本切换代码,这是一个脆弱的假设(即将来不会更改)。

您可以为该实例设置它,也可以为所有选项卡栏全局设置它:

// not supported on iOS4    
UITabBar *tabBar = [tabController tabBar];
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)])
{
    // set it just for this instance
    [tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar_brn.jpg"]];

    // set for all
    // [[UITabBar appearance] setBackgroundImage: ...
}
else
{
    // ios 4 code here
}
//---- For providing background image to tabbar

UITabBar *tabBar = [tabBarController tabBar];
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)])
{
    // ios 5 code here
    [tabBar setBackgroundImage:[UIImage imageNamed:@"PB_MD_footer_navBg_v2.png"]];

}
else
{
    // ios 4 code here

    CGRect frame = CGRectMake(0, 0, 480, 49);
    UIView *tabbg_view = [[UIView alloc] initWithFrame:frame];
    UIImage *tabbag_image = [UIImage imageNamed:@"PB_MD_footer_navBg_v2.png"];
    UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image];
    tabbg_view.backgroundColor = tabbg_color;
    [tabBar insertSubview:tabbg_view atIndex:0];

}

我知道此问题已解决,因此我将其发布给与我有相同问题的其他人。 我想将背景图像添加到标签栏中的所选标签中。 解决方法如下:

[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabbar.png"]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar-item.png"]];

这里的第二行将背景图像添加到标签栏中的所选标签中。

阅读各种文章后,我找到了遇到相同问题的任何人的答案:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBG.png"]];

if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
    //iOS 5
    [self.tabBarController.tabBar insertSubview:imageView atIndex:1];
}
else {
    //iOS 4.whatever and below
    [self.tabBarController.tabBar insertSubview:imageView atIndex:0];
}

[imageView release];

奇迹般有效! 请享用。

iOS 5中有forBarMetrics:UIBarMetricsDefaultforBarMetrics:UIBarMetricsDefault

这是苹果文件

[[UINavigationBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault];

暂无
暂无

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

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