无法在iOS 13中设置选项卡栏阴影图片。

9

iOS13之前,我使用下面的代码来移除选项卡顶部边框:

UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()

但是它不适用于iOS13,我正在寻找解决方案。你有什么想法吗?

2个回答

18

Swift 4+:

在您的TabBarController类中编写以下代码:

 if #available(iOS 13, *) {
        let appearance = self.tabBar.standardAppearance.copy()
        appearance.backgroundImage = UIImage()
        appearance.shadowImage = UIImage()
        appearance.shadowColor = .clear
        self.tabBar.standardAppearance = appearance
    } else {
        self.tabBar.shadowImage = UIImage()
        self.tabBar.backgroundImage = UIImage()
    }

要进行标题调整,请使用以下内容:

appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -12)

针对Objective C

  if (@available(iOS 13.0, *)) {
    UITabBarAppearance* appearance =  self.tabBar.standardAppearance.copy;
    appearance.backgroundImage = [UIImage new];
    appearance.shadowImage = [UIImage new];
    appearance.shadowColor = [UIColor clearColor];
    // Title adjustment
    appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffsetMake(0, -12);
    self.tabBar.standardAppearance = appearance;
} else {
    self.tabBar.shadowImage = [UIImage new];
    self.tabBar.backgroundImage = [UIImage new];
}

@SargisTerteryan能否提供以上代码的Objective-C版本? - Yogendra Patel
@SargisTerteryan 谢谢您的回答,但是它不起作用。因为在tabbar中找不到“standardAppearance”属性。如果您有其他解决方案,请帮助我。 - Yogendra Patel
@SargisTerteryan 好的,当然。 - Yogendra Patel
如何使用此方法为UITabBarItem设置titleTextAttributes? - dharam
2
@dharam 你可以从UITabBarItem获取标准外观的副本,然后更改它的外观。stackedLayoutAppearance.normal.titleTextAttributes = "你的属性" - Sargis Terteryan
显示剩余9条评论

2
在iOS 13中,您可以使用基于外观的方法,并使用内置方法配置透明度:
    if #available(iOS 13, *) {
        let appearance = self.tabBar.standardAppearance.copy()
        appearance.configureWithTransparentBackground()
        tabBar.standardAppearance = appearance
    } else {
        tabBar.backgroundImage = UIImage()
        tabBar.shadowImage = UIImage()
        tabBar.barTintColor = UIColor.clear
    }

要将其改回来,您可以使用configureWithDefaultBackground()执行相同的操作:

    if #available(iOS 13, *) {
        let appearance = self.tabBar.standardAppearance.copy()
        appearance.configureWithDefaultBackground()
        tabBar.standardAppearance = appearance
    } else {
        tabBar.barTintColor = nil
        tabBar.backgroundImage = nil
        tabBar.shadowImage = nil
    }

当我想要根据主题更改外观时,它无法正常工作。你知道吗? - vikas prajapati

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