Abh*_*tra 4 uitabbar ios swift
我需要将阴影效果放入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
Run Code Online (Sandbox Code Playgroud)
它运作得很好.
但是,我需要删除顶部的边框UITabBar,并通过搜索我得到self.tabBar.clipsToBounds = true,通过放置代码,它删除边框,但它也删除阴影效果.
我需要像下面的图片:
没有边框,但有阴影效果.
任何帮助将不胜感激.
Aka*_*ndu 11
所以@Reinier Melian 提供的答案对我不起作用,但我制作了一个自定义 TabBar 控制器,这种效果有效:
为 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()
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用它:
要使用它,将一个标签栏控制器拖到故事板上,然后通过下拉菜单将该标签栏的类更改为这个。
您需要添加一个UIView在您的TabBar,并.shadowImage和.backgroundImage等于UIImage()
码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let tabBarController = self.window?.rootViewController as? UITabBarController {
let tabGradientView = UIView(frame: tabBarController.tabBar.bounds)
tabGradientView.backgroundColor = UIColor.white
tabGradientView.translatesAutoresizingMaskIntoConstraints = false;
tabBarController.tabBar.addSubview(tabGradientView)
tabBarController.tabBar.sendSubview(toBack: tabGradientView)
tabGradientView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tabGradientView.layer.shadowOffset = CGSize(width: 0, height: 0)
tabGradientView.layer.shadowRadius = 4.0
tabGradientView.layer.shadowColor = UIColor.gray.cgColor
tabGradientView.layer.shadowOpacity = 0.6
tabBarController.tabBar.clipsToBounds = false
tabBarController.tabBar.backgroundImage = UIImage()
tabBarController.tabBar.shadowImage = UIImage()
}
// Override point for customization after application launch.
return true
}
Run Code Online (Sandbox Code Playgroud)
结果