[iOS diary]iOS6樣式相容之 NavigationBar和TabBar 樣式扁平化 相容iOS7樣式 全域實現

來源:互聯網
上載者:User

一篇跟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{}}



結果圖如下
































相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.