iOS 7的tabBar线条如何去除?

43

iOS 7中,苹果在tabBar上面添加了一条微小的线条,旨在作为tabBar和UI之间的阴影或淡出效果。

图片描述

由于我正在使用自定义的tabBar,这条线让人感到非常烦人。如何去掉它?请告诉我这是可能的,否则我需要重新设计我的整个应用程序....

/问候

*编辑

通过以下代码解决了我的问题:

[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];

10
[_tabBar setClipsToBounds:TRUE]; - holex
1
holex的答案对我来说就像奇迹一样有效 ^_^ - Pradeep Mittal
3
可能是 Remove UITabBar horizontal separator in iOS7 的重复内容。 - QED
12个回答

22
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar_bg.png"];
    [[UITabBar appearance] setShadowImage:tabBarBackground];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];   

4
注意:如果您使用setShadowImage方法,它会占据Tab Bar Controller上方的一些空间。最好使用[[UITabBar appearance] setShadowImage:[[UIImage alloc] init];来提供一个空的透明UIImage - Raptor
@Raptor 你是个天才。 - DawnSong

14

对我来说这些代码运行得相当好(我没有为选项卡栏设置背景图像):

[tab_main.tabBar setBackgroundImage:[[UIImage alloc] init]];
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];

我使用这些代码也可以添加一个框架:

UIColor* color_green = UIColorFromRGB(0x348e5b);
tab_main.tabBar.layer.borderWidth = 0.50;
tab_main.tabBar.layer.borderColor = color_green.CGColor;
[[UITabBar appearance] setTintColor:color_green];
希望这有所帮助。

7
在iOS 8中,可以通过在检查器中将选项卡栏样式设置为黑色来移除顶部边框。

1
好的,它并不会将其删除,而是使其变成白色。我有一个很大的中心按钮,它正好穿过它。 - Raheel Sadiq

5

Swift

简单易行的解决方案:

在你的自定义选项卡栏类中添加以下代码即可隐藏水平阴影线。

self.tabBar.setValue(true, forKey: "_hidesShadow")

Objective C

[self.tabBar setValue:@(YES) forKeyPath:@"_hidesShadow"];

私有API不是首选。 - DawnSong

1
self.tabBarController =  [[UITabBarController alloc] init];
[[[self tabBarController] tabBar] setBackgroundImage:[UIImage imageNamed:@"YOURIMAGE.png"]];
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];

这段代码很令人困惑。变量名为tabBar,但你在其上初始化了UITabBarController - Raptor
我的建议是将变量重命名为 tabBarController,以避免混淆。 - Raptor
tabBar是UITabBarController的对象。也就是说,@property (nonatomic, retain) UITabBarController* tabBar; 我已经将tabBar的名称更改为tabBarController(@property (nonatomic, retain) UITabBarController* tabBarController)。现在你可以清楚地理解代码了,不会再感到疑惑。 - dineshthamburu

0

试试这个, ** Objective-C **

//Remove shadow image by assigning nil value.
[[UITabBar appearance] setShadowImage: nil];

// or 

// Assing UIImage instance without image reference
[[UITabBar appearance] setShadowImage: [[UIImage alloc] init]];

** Swift **

//Remove shadow image by assigning nil value.
UITabBar.appearance().shadowImage = nil

// or 

// Assing UIImage instance without image reference
UITabBar.appearance().shadowImage = UIImage()


这里是苹果文档shadowImage

@available(iOS 6.0, *)
open var shadowImage: UIImage?

默认值为nil。当非nil时,将显示自定义阴影图像而不是默认阴影图像。要显示自定义阴影,还必须使用-setBackgroundImage:设置自定义背景图像(如果使用默认背景图像,则将使用默认阴影图像)。

0

这对我有用

UIImage* tabBarBackground = [UIImage new];
if(!OSVersionIsAtLeastiOS7())
{
    tabBarBackground = [UIImage imageNamed:@"whitebg"];
}
[[UITabBar appearance] setShadowImage:tabBarBackground];

[[UITabBar appearance] setBackgroundImage:tabBarBackground];

0
将以下代码添加到AppDelegate.mdidFinishLaunchingWithOptions:方法中。
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
 [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];

0

我在UITabBar API中没有找到任何影响分隔符的内容,但是如果分隔符在UITabBar中(一个UIView子类),我希望你可以在其上方插入一个新的一像素高的UIView。你需要截取想要显示的图像部分,并在新的视图中绘制它。而且我不确定UITabBar是否会阻止添加子视图或阻止子视图处于顶层。但这是我会开始的地方。


3
只要您也设置了一个背景图像,将shadowImage设置为透明图像就可以消除它。 - iccir

0
 [_tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tabtarsprnt"]]; //your custom image
[self.tabBarController.tabBar setClipsToBounds:YES];

这段代码也解决了我的问题


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