iOS 8中完全透明的UITabBar

15

我想让我的tabBar透明,我搜索了一下,但是所有的结果都只是部分透明而不是完全透明的tabBar,有些还是为IOS 5等。

我想达到Sketch 3中所显示的效果:

enter image description here

最简单的方法是什么?

我考虑过这样做:

    // Make the tabBar transparent
self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];
self.tabBarController.tabBar.translucent = YES;

但是那个结果并不完美:

enter image description here

非常感谢帮助!:)

真诚地, Erik

更新

// Make the tabBar transparent
[[UITabBar appearance] setBarTintColor:[UIColor clearColor]];
self.tabBarController.tabBar.translucent = YES;
4个回答

33

你尝试过使用barTintColor吗?

[[UITabBar appearance] setBarTintColor:[UIColor clearColor]];
[[UITabBar appearance] setBackgroundImage:[UIImage new]];

那应该就行了。


与我在更新的问题中放置的代码结果相同 :/ - 我将这段代码放在viewWillAppear方法中,这样正确吗? - Erik
1
@Erik viewWillAppear: 太晚了。尝试将其放在您的AppDelegate的application:didFinishLaunchingWithOptions:中,看看是否有帮助。 - Johannes Fahrenkrug
@JohannesFagrenkrug 很不幸,我在didFinishLaunchingWithOptions中更新的代码得到了相同的结果。 - Erik
1
你真是节省了我很多时间。 - Oleg Gordiichuk
这个方法可行,但是标签栏顶部仍有一条黑线,你有什么办法可以去掉它吗? - Trianna Brannon
3
我将与Lior的答案结合使用,setShadowImage是消除黑线的技巧。 - Trianna Brannon

32

Swift 3.0

... 在AppDelegate的didFinishLaunchingWithOptions方法中调用此代码

let tabBar = UITabBar.appearance()
tabBar.barTintColor = UIColor.clear
tabBar.backgroundImage = UIImage()
tabBar.shadowImage = UIImage()

每个UITabBar的结果将是透明背景。


经过尝试了解互联网上的不同方法和解决方案,这使得选项卡栏变得“半透明”,但现在感觉它太透明了,完全没有模糊效果。我相信是 tabBar.backgroundImage = UIImage() 导致了这种情况。是否有其他帮助可以产生漂亮的模糊效果而不是完全透明? - C0D3

6

您需要继承UITabBarController,在viewdidload:方法中加入以下代码:

CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);
UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.tabBar setBackgroundImage:transparentImage];
[self.tabBar setShadowImage:transparentImage];    
//    self.tabBar.alpha = 0.0;

2
我没有使用子类,但是 setShadowImage 是我解决问题的秘诀,加1 :) - Olie

1
我使用以下代码解决了这个问题:
let tabBarController = UITabBarController()
...    
tabBarController.tabBar.backgroundImage = UIImage()
tabBarController.tabBar.barTintColor = .clear
tabBarController.tabBar.isTranslucent = true
            
tabBarController.extendedLayoutIncludesOpaqueBars = true
tabBarController.edgesForExtendedLayout = .all

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接