想要打造與內容融為一體的沉浸式底部導覽列,可以透過設定透明背景來實現。以下直接看範例程式碼。
// 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;
輔助方法:產生指定顏色的純色圖片,這裡用來產生透明圖以覆蓋系統預設的背景與陰影:
-(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;
}
透過上述設定,即可讓 TabBar 呈現透明背景並與頁面內容自然融合,希望這篇文章對你的開發有所幫助。