ios - UITabBar 边框和阴影问题

标签 ios swift uitabbar

我需要在 UITabBar 中添加阴影效果,我通过以下代码获得:

tabBar.layer.shadowOffset = CGSize(width: 0, height: 0)
tabBar.layer.shadowRadius = 4.0
tabBar.layer.shadowColor = UIColor.gray.cgColor
tabBar.layer.shadowOpacity = 0.6

enter image description here

而且它运行良好。

但是,我需要移除 UITabBar 顶部的边框,通过搜索我得到了 self.tabBar.clipsToBounds = true,通过放置该代码,它删除了边框,但也删除了阴影效果。

enter image description here

我需要如下图:

enter image description here

无边框但有阴影效果。

任何帮助将不胜感激。

最佳答案

所以@Reinier Melian 提供的答案对我不起作用,但我制作了一个具有这种效果的自定义 TabBar Controller :

针对 Swift 5、iOS 13、Xcode 11 进行了更新

代码:

class MyCustomTabBarController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self

        //here's the code that creates no border, but has a shadow:

        tabBar.layer.shadowColor = UIColor.lightGray.cgColor
        tabBar.layer.shadowOpacity = 0.5
        tabBar.layer.shadowOffset = CGSize.zero
        tabBar.layer.shadowRadius = 5
        self.tabBar.layer.borderColor = UIColor.clear.cgColor
        self.tabBar.layer.borderWidth = 0
        self.tabBar.clipsToBounds = false
        self.tabBar.backgroundColor = UIColor.white
        UITabBar.appearance().shadowImage = UIImage()
        UITabBar.appearance().backgroundImage = UIImage()
    }
}

如何使用它:

要使用它,请将标签栏 Controller 拖到 Storyboard 上,然后只需通过下拉菜单将该标签栏的类更改为此。

关于ios - UITabBar 边框和阴影问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49528913/

相关文章:

ios - 通过在 Swift 中添加两个标签而不是标题来自定义导航栏

ios - 通过 Mono 框架在 iOS 应用程序中使用现有的 DLL

ios - iOS 中使用 FCM 的架构 arm64 的错误 undefined symbol

ios - UITabBar 不改变其背景颜色

ios - UITabBar 标题十六进制值

ios - 尝试将 CMTime 存储为核心数据上的可转换属性

ios - 更新的应用程序将删除现有文件

ios - 如何本地化我的 UITabBarItems?

swift - 如何获取嵌套类的字符串表示形式

arrays - Swift3 - 扩展类型 Collection 的问题