iOS 13 选项卡栏层属性丢失 swift

iOS 13 Tab Bar Layer attribute is missing swift

我在Layer的初始版本中在Tab Bar上添加了阴影,但是在iOS13我们必须使用没有图层属性的Appearance,现在如何添加阴影。

顶部的阴影如下图。

if #available(iOS 13, *) {
            let appearance = self.self.tabBarController?.tabBar.standardAppearance.copy()
            appearance?.backgroundImage = UIImage()
            appearance?.shadowImage = UIImage()
            appearance?.shadowColor = .clear
            //appearance?.layer this is missing now
            if let appearance = appearance{
                self.tabBarController?.tabBar.standardAppearance = appearance
            }

        } else {
        self.tabBarController?.tabBar.shadowImage = UIImage()
        self.tabBarController?.tabBar.backgroundImage = UIImage()
        self.tabBarController?.tabBar.layer.shadowOpacity = 0.0
        self.tabBarController?.tabBar.layer.borderWidth = 0.0
        self.tabBarController?.tabBar.clipsToBounds = true
        self.tabBarController?.tabBar.layer.applySketchShadow(color: UIColor(red: 15/255, green: 54/255, blue: 136/255, alpha: 1.0), alpha: 0.1, x: 0, y: 0, blur: 25, spread: 0)
        self.tabBarController?.tabBar.clipsToBounds = false
        }

UIBarAppearance 具有 shadowImageshadowColor 属性。根据需要设置它们。现在您将它们设置为 no 阴影。 另外 configureWithTransparentBackground 表示 没有 影子。所以你在去掉影子,然后问影子在哪里。

在您的 UITabBarController class

中尝试以下代码
 override func viewDidLoad() {
        super.viewDidLoad()

      if #available(iOS 13, *) {
            let appearance = self.tabBar.standardAppearance.copy()
            appearance.backgroundImage = UIImage()
            appearance.shadowImage = UIImage()
            self.tabBar.standardAppearance = appearance
        } else {
            self.tabBar.shadowImage = UIImage()
            self.tabBar.backgroundImage = UIImage()
        }

        //Change These values according to your requirement. This will work for all iOS versions
        self.tabBar.layer.shadowColor = UIColor.lightGray.cgColor
        self.tabBar.layer.shadowOpacity = 0.3
        self.tabBar.layer.shadowRadius = 5
}

设置 clipsToBounds False 将为您带来神奇效果。

//**self.tabBarController?.tabBar.clipsToBounds = false**

if #available(iOS 13, *) {
    let appearance = self.self.tabBarController?.tabBar.standardAppearance.copy()
    appearance?.backgroundImage = UIImage()
    appearance?.shadowImage = UIImage()
    appearance?.configureWithTransparentBackground()
    if let appearance = appearance{
        self.tabBarController?.tabBar.standardAppearance = appearance
    }

} else {
self.tabBarController?.tabBar.shadowImage = UIImage()
self.tabBarController?.tabBar.backgroundImage = UIImage()
self.tabBarController?.tabBar.clipsToBounds = false

}
self.tabBarController?.tabBar.clipsToBounds = false
self.tabBarController?.tabBar.layer.shadowOpacity = 0.0
self.tabBarController?.tabBar.layer.borderWidth = 0.0
self.tabBarController?.tabBar.layer.applySketchShadow(color: UIColor(red: 15/255, green: 54/255, blue: 136/255, alpha: 1.0), alpha: 0.1, x: 0, y: 0, blur: 25, spread: 0)