您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 實作自訂透明背景的 tabbar

編輯:iOS 相關教學

直接看程式碼。

// UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, tabBarHeight + 5)];
// [imageView setImage:[self createImageWithColor:[UIColor clearColor]]];
// [imageView setContentMode:UIViewContentModeScaleToFill];
// [self.tabBar insertSubview:imageView atIndex:0];

// 蓋掉原生 Tabbar 頂部的橫線
[[UITabBar appearance] setShadowImage:[self createImageWithColor:[UIColor clearColor]]];

// 背景設為透明
[[UITabBar appearance] setBackgroundImage:[self createImageWithColor:[UIColor clearColor]]];

self.tabBar.backgroundColor = [UIColor clearColor];

// 設為半透明
self.tabBarController.tabBar.translucent = YES;

其中產生純色( here 是透明)影像的小工具方法如下:

- (UIImage *)createImageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

幾點提醒

1. setBackgroundImage: 與 setShadowImage: 接受的是 UIImage,所以我們用一段 1×1 的透明影像去「塗掉」系統預設的背景與分隔線,這是 iOS 7 之後常見的手法。

2. 若使用 [[UITabBar appearance] ...],設定會全域生效;只想影響單一 tabbar 時,把 appearance 換成該 tabBar 實例即可。

3. 透明 tabbar 之後,記得幫內容頁加上底部內距(例如調整 edgesForExtendedLayout 或 contentInset),否則最後一個 cell 會被導覽列遮住。

以上就是實作透明背景 tabbar 的完整做法,希望對大家有幫助。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved