我需要在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
,通过放置该代码,它可以删除边框,但也可以删除阴影效果。
我需要像下面的图像:
无边框但有阴影效果。
任何帮助将不胜感激。
最佳答案
您需要在TabBar中添加UIView
,并使.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
}
结果
关于ios - UITabBar边框和阴影问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49528913/