【问题标题】:iOS UITabBar : Remove top shadow gradient lineiOS UITabBar:删除顶部阴影渐变线
【发布时间】:2012-12-31 12:50:57
【问题描述】:

我实现了一个自定义的 UITabBar,我仍然在它上面有这个渐变/阴影。 我加了

[self.tabBar setBackgroundImage:[UIImage imageNamed:@"navBarBottom.png"]];

这只是改变背景但保持阴影渐变。

我做错了什么?有什么可以摆脱它的指定吗?

我有什么:

我想要什么:

谢谢。

【问题讨论】:

标签: iphone ios uitabbarcontroller uitabbar


【解决方案1】:

this question 的回答类似 ...如果您不想弄乱任何类型的 1x1 透明图像,那么这项工作也是如此:

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

迅速:

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

【讨论】:

  • 别忘了:[[UITabBar 外观] setBackgroundImage:[[UIImage alloc] init]];
  • @Roger:我不确定,如果你只需要移除阴影,是否有必要?
  • 是的。我必须这样做才能完全消除阴影,否则它仍然会显示它。
  • @RogerFernandezGuri,实际上它并没有显示阴影,但您看到的是标签栏背景颜色。而不是 '[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];',您也可以尝试设置背景颜色...
  • 适用于 iOS 12。但不再适用于 iOS13!有人试过吗?有一个新的tabBar.standardAppearance.shadowImage,但它根本不起作用......
【解决方案2】:

尝试为 UITabBar 设置一个 1x1 像素的透明阴影图像:

[[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"transparentShadow.png"]];

【讨论】:

  • 我知道如何处理 UITabBarItem 阴影并已将其删除。我担心的是许多应用程序成功移除的 UITabBar 阴影(顶部的大线)。
  • 成功了,谢谢 :) (也适用于想知道 transparentShadow.png 是 1x1 透明图像的人)
  • 谢谢伙伴..你的代码解决了很多尝试..再次感谢。
  • 重要提示:“setShadowImage”的值默认被忽略,除非您还使用 setBackgroundImage 设置了背景图像。所以你总是必须同时设置! [来自 Apple 文档]
【解决方案3】:

斯威夫特

为您的自定义标签栏试试这个。它会隐藏水平阴影线。

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

目标 C

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

【讨论】:

  • 您也可以在“用户定义的运行时属性”部分下的 UITabBar 的 Identity Inspector 窗格中的 Interface Builder 中添加它。点击 + 添加一行并输入 _hidesShadow 键和值。
  • 这是访问私有 API,因此可能会被拒绝,并且可能会在未来的 iOS 版本中中断。
  • 我使用 Swift 代码。它适用于 2018 年 4 月的 iOS 11。
【解决方案4】:

斯威夫特 4

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

【讨论】:

  • 只用UITabBar.appearance().clipsToBounds = true也有同样的效果。
【解决方案5】:

此代码适用于 iOS 13 及以下版本

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.backgroundImage = UIImage()
    self.tabBar.shadowImage = UIImage()
}

【讨论】:

  • 如果您在 UITabBarItem 的文本上有自定义字体,使用您的方法会受到影响
【解决方案6】:

调用[[UITabBar appearance] setShadowImage:] 将自定义您应用中的所有UITabBar 实例。

如果你只想自定义一个UITTabBar,你可以这样做:

[self.tabBarController.navigationController.navigationBar setShadowImage:[UIImage new]];

【讨论】:

    【解决方案7】:

    这是另一个易于实施的答案:

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

    为我工作。

    【讨论】:

    • 需要注意的是,这似乎没有记录。最好使用上面的 - 公认更乏味的 - 方法,设置阴影和背景图像。
    【解决方案8】:

    只是设置图像它不会删除阴影线,你必须将它的边框宽度设置为 0。这是代码

    [[UITabBar 外观] setShadowImage:[UIImage new]];

    [UITabBar 外观].layer.borderWidth = 0.0f;

    [UITabBar 外观].clipsToBounds = true;

    【讨论】:

      【解决方案9】:

      将其放在 didFinishLaunchingWithOptions 下的 AppDelegate 中:

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

      【讨论】:

        【解决方案10】:

        试试这个, ** 目标-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 设置:(如果 使用默认背景图像,默认阴影图像将是 使用)。

        【讨论】:

          【解决方案11】:

          我通过以下方法实现了相同的外观。
          1. 将背景栏色调设置为与主父视图背景颜色相同。
          2.

          this.TabBar.BarStyle = UIBarStyle.BlackOpaque;
          

          我在 Xamarin 中使用过,请验证 Swift 语法。

          【讨论】:

            【解决方案12】:

            如果您需要从具有自定义字体的选项卡栏中删除 iOS 13 上的阴影线,那么您必须以这种方式应用它:

            if #available(iOS 13.0, *) {
               let appearance = UITabBarAppearance()
               appearance.stackedLayoutAppearance.normal.titleTextAttributes = ...
               appearance.stackedLayoutAppearance.selected.titleTextAttributes = ...
               appearance.shadowColor = .clear
               tabBar.standardAppearance = appearance
             }
            
            

            【讨论】:

              【解决方案13】:

              在大多数视图控制器在 viewDidLoad 中继承的视图控制器或视图控制器或 BasicViewController 中,只需放置以下两行:

              [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tab_bar_background"]];
              [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"transparent_shadow"]];
              

              确保 transparent_shadow@2x.png 是 1x1 或 2x2 透明的图像,并且 tab_bar_background@2x.png 是 640x100 的图像,因为底栏的高度为 50 像素。

              适用于 iOS 9.3

              【讨论】:

                【解决方案14】:

                在 viewDidload 上试试这个。

                override func viewDidLoad() {
                        super.viewDidLoad()
                
                        self.tabBar.setValue(true, forKey: "_hidesShadow")
                }
                

                对我有用

                【讨论】:

                  【解决方案15】:

                  在 iOS 7 中 - 这有效:

                  [self.actionToolbar setShadowImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionAny];
                  [self.actionToolbar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
                  

                  希望对某人有所帮助。

                  【讨论】:

                  • 它是“UITabBar”而不是 UIToolBar。
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-05-07
                  • 2016-02-01
                  • 2011-08-30
                  • 1970-01-01
                  • 2017-11-18
                  • 1970-01-01
                  相关资源
                  最近更新 更多