[iOS diary]iOS6样式兼容之 NavigationBar和TabBar 样式扁平化 兼容iOS7样式 全局实现

一篇跟iOS6 样式扁平化的好文章:http://www.tuicool.com/articles/6NFfYj

[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]]; 去掉后背的黑色背景 或 在

[[UITabBar appearance] setTintColor:[UIColor clearColor]];时去掉后上方的粗黑边

[[UITabBar appearance]setShadowImage:[[UIImagealloc]init]];去掉上方的浅阴影。


iOS7兼容iOS6选中图片

UITabBarItem *item = [[UITabBarItemalloc]initWithTitle:@"直播"image:[UIImageimageNamed:@"tab_icon_home_normal"] tag:0];

if([[[UIDevicecurrentDevice]systemVersion]floatValue] >= 7.0)

{

[itemsetSelectedImage:[UIImageimageNamed:@"tab_icon_home_selected"]];

}

else

{

[itemsetFinishedSelectedImage:[UIImageimageNamed:@"tab_icon_home_selected"]withFinishedUnselectedImage:[UIImageimageNamed:@"tab_icon_home_normal"]];

}


以下为对iOS6 NavigationBar与TabBar 样式扁平化的代码。


1、对UINavigationBar 与 UITabBar的全局样式修改,写在AppDelegate LaunchFinish中

- (void)setupUIAppearance
{
	[[UITabBar appearance] setSelectedImageTintColor:[UIColor colorWithRed:81.0/255 green:196.0/255 blue:212.0/255 alpha:1]];
	[[UITabBarItem appearance] setTitleTextAttributes:
	 @{ UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
		UITextAttributeTextColor: [UIColor lightGrayColor] }
											 forState:UIControlStateSelected];
	[[UITabBarItem appearance] setTitleTextAttributes:
	 @{ UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
		UITextAttributeTextColor: [UIColor lightGrayColor] }
											 forState:UIControlStateNormal];
	UIColor *navigationBarColor = [UIColor colorWithRed:81.0/255 green:196.0/255 blue:212.0/255 alpha:1];
	if(floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
	{
		[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
		[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
		[[UITabBar appearance] setSelectedImageTintColor:[UIColor clearColor]];
		[[UINavigationBar appearance] setTintColor:navigationBarColor];
		[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
		[[UIBarButtonItem appearance] setBackgroundImage:[UIImage new]
												forState:UIControlStateNormal
											  barMetrics:UIBarMetricsDefault];
		[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:navigationBarColor size:CGSizeMake(1, 44)]
										   forBarMetrics:UIBarMetricsDefault];
		[[UINavigationBar appearance] setTitleTextAttributes:
		 @{ NSForegroundColorAttributeName: [UIColor whiteColor],
			NSFontAttributeName: [UIFont boldSystemFontOfSize:20],
			UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero]}];

		[[UIBarButtonItem appearance] setTitleTextAttributes:
         @{ UITextAttributeFont: [UIFont systemFontOfSize:17],
            UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero]} forState:UIControlStateNormal];

		[[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 49)]];

		[[UITabBar appearance] setSelectionIndicatorImage:[UIImage new]];

	}
	else
	{
		[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:81.0/255 green:196.0/255 blue:212.0/255 alpha:1]];
	}
}

2、TabBar图片设置如下

- (void)setupTabBarItemsAndViewControllers
{
	

	NSMutableArray *mutableTabBarItems = [NSMutableArray array];
	NSMutableArray *mutableViewControllers = [NSMutableArray array];
	
	CCMoreOptionsViewController *moreOptionsViewController = [[CCMoreOptionsViewController alloc] initWithNibName:nil bundle:nil];

	for(NSInteger i = 0; i < CC_TAB_BAR_ITEM_TYPE_TOTAL_NUMBER ; i++)
	{
		switch (i) {
			case CCTabBarItemTypeLiveShow:
			{
				UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"xxxx" image:[UIImage imageNamed:@"tab_icon_home_normal"] tag:0];
				if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
				{
					[item setSelectedImage:[UIImage imageNamed:@"tab_icon_home_selected"]];
				}
				else
				{
					[item setFinishedSelectedImage:[UIImage imageNamed:@"tab_icon_home_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_icon_home_normal"]];
				}
				[mutableTabBarItems addObject:item];
				UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:moreOptionsViewController];
				vc.tabBarItem = item;
				[mutableViewControllers addObject:vc];
				break;
			}

			case CCTabBarItemTypeMessage:
			{
				UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"消息" image:[UIImage imageNamed:@"tab_icon_message_normal"] tag:1];
				if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
				{
					[item setSelectedImage:[UIImage imageNamed:@"tab_icon_message_selected"]];
				}
				else
				{
					[item setFinishedSelectedImage:[UIImage imageNamed:@"tab_icon_message_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_icon_message_normal"]];
				}
				[mutableTabBarItems addObject:item];
				UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:moreOptionsViewController];
				vc.tabBarItem = item;
				[mutableViewControllers addObject:vc];
				break;
			}
			case CCTabBarItemTypeMobileGameVideo:
			{
				UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"xxxx" image:[UIImage imageNamed:@"tab_icon_video_normal"] tag:1];
				if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
				{
					[item setSelectedImage:[UIImage imageNamed:@"tab_icon_video_selected"]];
				}
				else
				{
					[item setFinishedSelectedImage:[UIImage imageNamed:@"tab_icon_video_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_icon_video_normal"]];
				}
				[mutableTabBarItems addObject:item];
				UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:moreOptionsViewController];
				vc.tabBarItem = item;
				[mutableViewControllers addObject:vc];
				break;
			}
			case CCTabBarItemTypeMoreOptions:
			{
				UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"更多" image:[UIImage imageNamed:@"tab_icon_more_normal"] tag:1];
				if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
				{
					[item setSelectedImage:[UIImage imageNamed:@"tab_icon_more_selected"]];

				}
				else
				{
					[item setFinishedSelectedImage:[UIImage imageNamed:@"tab_icon_more_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_icon_more_normal"]];
				}
				[mutableTabBarItems addObject:item];
				UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:moreOptionsViewController];
				vc.tabBarItem = item;
				[mutableViewControllers addObject:vc];
				break;
			}
			default:
				break;
		}
	}

	self.viewControllers = mutableViewControllers;
}

3、设置NavigationBar如下

- (void)setupNavigationBar
{
	BOOL isLogin = YES;
	if(isLogin)
	{
		CGRect rect = CGRectMake(0, 0, 30, 30);
		UIButton *headPhotoButton = [[UIButton alloc] initWithFrame:rect];
		[headPhotoButton setImage:[UIImage imageNamed:@"icon_default_avatar"] forState:UIControlStateNormal];
		UIButton *historyButton = [[UIButton alloc] initWithFrame:rect];
		[historyButton setImage:[UIImage imageNamed:@"nav_icon_history_normal"] forState:UIControlStateNormal];
		UIButton *searchButton = [[UIButton alloc] initWithFrame:rect];
		[searchButton setImage:[UIImage imageNamed:@"nav_icon_search_normal"] forState:UIControlStateNormal];
		[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:headPhotoButton]];
		[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithCustomView:historyButton],[[UIBarButtonItem alloc] initWithCustomView:searchButton], nil]];
	}
	else
	{

	}
}



结果图如下

事例图
































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值