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

来源:互联网 发布:java电商架构 编辑:程序博客网 时间:2024/05/01 01:42

一篇跟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 0
原创粉丝点击