我需要在 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
而且它运行良好。
但是,我需要移除 UITabBar
顶部的边框,通过搜索我得到了 self.tabBar.clipsToBounds = true
,通过放置该代码,它删除了边框,但也删除了阴影效果。
我需要如下图:
无边框但有阴影效果。
任何帮助将不胜感激。
最佳答案
所以@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/