从 TabBar 中删除顶行

Remove top line from TabBar

在 iOS 10 上,此代码无法用于删除 tabBar 阴影线:

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

有人知道,我必须怎么做才能删除它?

iOS 9.3 上,这两行被删除,但 iOS 10 忽略 setShadowImage 命令。

只需尝试下面的代码 iOS 10 :-

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"fondoTabBar"]];
    [UITabBar appearance].layer.borderWidth = 0.0f;
    [UITabBar appearance].clipsToBounds = true;
    return YES;
}

Swift 3.x

UITabBar.appearance().layer.borderWidth = 0.0
UITabBar.appearance().clipsToBounds = true

对于 iOS 10,将标签栏样式更改为黑色就成功了

self.tabBarController.tabBar.shadowImage = UIImage()
self.tabBarController.tabBar.barStyle = .Black

我在 ios 10 中遇到了同样的问题。我通过更改 UITabBar 的高度(默认为 49)解决了这个问题。检查here如何改变高度。

您应该同时实现以下两种方法:

[[UITabBar appearance] setShadowImage:[UIImage new]];
[[UITabBar appearance] setBackgroundImage:[UIImage new]];

如果您创建自己的 UITabBarController 子类,您可以设置 viewDidLoad 中的值是这样的

Swift 3

override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBar.layer.borderWidth = 0
        self.tabBar.clipsToBounds = true
}

这是标签栏的阴影图像(属性)。尝试以下解决方案并查看。

试试这个, ** 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?

Default is nil. When non-nil, a custom shadow image to show instead of the default shadow image. For a custom shadow to be shown, a custom background image must also be set with -setBackgroundImage: (if the default background image is used, the default shadow image will be used).

删除@iOS 13.0

的顶线
let appearance = tabBar.standardAppearance
appearance.shadowImage = nil
appearance.shadowColor = nil
tabBar.standardAppearance = appearance;

删除 iOS 12.0 及更早版本

的顶线
tabBar.shadowImage = UIImage()
tabBar.backgroundImage = UIImage()

测试于 iOS 14

override func viewDidLoad() {
    // Remove default border line
    tabBar.shadowImage = UIImage()
    tabBar.backgroundImage = UIImage()
    tabBar.backgroundColor = UIColor.white
}

为iOS13

if (@available(iOS 13.0, *)) {
    UITabBarAppearance *appearance = [self.tabBarController.tabBar.standardAppearance copy];
    appearance.shadowImage = nil;
    appearance.shadowColor = nil;
    self.tabBarController.tabBar.standardAppearance = appearance;
}

我没有发表评论的名誉,但可以添加到 gnoix 的回答中,我有一个稍微不同的问题,因为我想要阴影 背景清晰所以我有在 Swift

if #available(iOS 13.0, *) {
    let appearance = tabBar.standardAppearance.copy()
    appearance.configureWithTransparentBackground()
    tabBar.standardAppearance = appearance
} else {
    tabBar.backgroundColor = UIColor.clear
    let image = UIImage(ciImage: CIImage(color: CIColor.clear)).af_imageAspectScaled(toFit: tabBar.bounds.size)
    tabBar.backgroundImage = image
    tabBar.shadowImage = image
}

这对我有用 @iso13:

AppDelegate.swift

UITabBar.appearance().clipsToBounds = true
UITabBar.appearance().shadowImage = nil

UITabBar.appearance().clipsToBounds = true
UITabBar.appearance().layer.borderWidth = 0

UITabBar.appearance().clipsToBounds = true
UITabBar.appearance().layer.borderColor = UIColor.clear.cgColor

您可以在 FirstViewController.swift:
Swift 4.2

self.tabBarController!.tabBar.layer.borderWidth = 0.50
self.tabBarController!.tabBar.layer.borderColor = UIColor.clear.cgColor
self.tabBarController?.tabBar.clipsToBounds = true

只需根据需要更改边框颜色即可。

截至 iOS 13.3

的工作解决方案
// remove top line
if #available(iOS 13.0, *) {
    // ios 13.0 and above
    let appearance = tabBar.standardAppearance
    appearance.shadowImage = nil
    appearance.shadowColor = nil
    appearance.backgroundEffect = nil
    // need to set background because it is black in standardAppearance
    appearance.backgroundColor = .someColor
    tabBar.standardAppearance = appearance
} else {
    // below ios 13.0
    let image = UIImage()
    tabBar.shadowImage = image
    tabBar.backgroundImage = image
    // background
    tabBar.backgroundColor = .someColor
}

对于我来说,带有 tabBar 外观的解决方案不起作用,因为在那种情况下它忽略了我的 tabBar.itemPositioning = .centered

对我有用的是tabBar.barStyle = .black

并且不要忘记设置 tabBar.backgroundColor = .white 或您喜欢的任何其他颜色。

如果你正在使用故事板,那是一种方式 在标签栏控制器上,select“标签栏”

然后添加一个运行时属性“clipsToBounds”bool: