【问题标题】:Remove top line from TabBar从 TabBar 中删除顶行
【发布时间】:2017-02-12 12:44:17
【问题描述】:

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

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

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

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

【问题讨论】:

  • 你能用图形显示吗?
  • shadowImage 文档的最后一行说,如果 backgroundImage 为 nil,则忽略 shadowImage 并应用默认阴影。所以尝试删除 setBackgroundImage 看看会发生什么。
  • 问题是我需要将图像设置为 tabBar 背景...
  • @mitulmarsonia 来自 iOs 10 模拟器的屏幕截图 i65.tinypic.com/24uw9vl.png

标签: ios ios10 tabbar


【解决方案1】:

删除@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()

【讨论】:

  • 不再适用于 iOS13!有人试过吗?有一个新的tabBar.standardAppearance.shadowImage,但它根本不起作用......
  • @SoftDesigner 我也有同样的问题,你找到 iOS 13 的解决方案了吗?
  • @YogendraPatel 我正在使用自定义阴影图像,我直接将其添加到标签栏:tabBar.addSubview(shadowImageView)。它与标准的 1px 线重叠。
  • 更新了 iOS 13.0 的答案
  • 外观弄乱了未选择的标签颜色
【解决方案2】:

请尝试为 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 之前使用的代码。此代码适用于以前的 iOs 10 设备,但在 iOs 10 上不起作用。
  • 你能试试这个演示它在 iOS 10 上的工作吗:- dropbox.com/sh/g4cpq282b7adyg0/AADk3jBHshvsQ-FNIWwzuOOZa?dl=0
  • 感谢您的示例代码。此代码适用于 iOs 10,但我需要更改 tabBar 背景,当我更改背景时会显示该行。这段代码是一样的,只是我添加了一个背景图像,你可以看到阴影图像在 iOs 10 上显示,在 iOs 9.3 上工作正常并且线被隐藏了。 1drv.ms/u/s!AlMk_BUOGnOImYtdCSCp6f1MGOu_AQ
  • @Camacho 我已经更新了我的答案,现在请查看您的问题已解决。
  • 感谢印度开发者社区,这不是您第一次帮助我解决有关 stackoverflow 的 iO 问题,非常感谢!
【解决方案3】:

适用于 iOS 13

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

【讨论】:

    【解决方案4】:

    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
    }
    

    【讨论】:

    • 这个答案应该是正确的,因为标记为正确的答案在应用外观时会改变标签栏的颜色。
    【解决方案5】:

    在 iOS 14 上测试

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

    【讨论】:

      【解决方案6】:

      这对我有用@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
      

      【讨论】:

        【解决方案7】:

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

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

        【讨论】:

          【解决方案8】:

          您可以在 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
          

          根据需要更改边框颜色。

          【讨论】:

            【解决方案9】:

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

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

            【讨论】:

              【解决方案10】:

              我没有评论的声誉,但要添加到 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
              }
              

              【讨论】:

              • appearance.configureWithTransparentBackground() 对我有用,因为我在 tabBar 层上也有阴影。
              • 问题是关于删除标签栏的顶线,而不是关于使标签栏背景透明。
              【解决方案11】:

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

              【讨论】:

                【解决方案12】:

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

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

                ** 斯威夫特 **

                //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?
                

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

                【讨论】:

                  【解决方案13】:

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

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

                  别忘了设置tabBar.backgroundColor = .white 或您喜欢的任何其他颜色。

                  【讨论】:

                    【解决方案14】:

                    如果您使用情节提要,那是一种方式 在标签栏控制器上,选择“标签栏”

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

                    【讨论】:

                      【解决方案15】:

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

                      斯威夫特 3

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

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2017-04-13
                        • 1970-01-01
                        • 2011-11-04
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多