当前位置 : 主页 > 手机开发 > ios >

ios – UITabBar边界和阴影问题

来源:互联网 收集:自由互联 发布时间:2021-06-11
我需要在UITabBar中设置阴影效果,我通过以下代码得到: tabBar.layer.shadowOffset = CGSize(width: 0, height: 0)tabBar.layer.shadowRadius = 4.0tabBar.layer.shadowColor = UIColor.gray.cgColortabBar.layer.shadowOpacity = 0.6 它
我需要在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

没有边框,但有阴影效果.

任何帮助将不胜感激.

你需要在你的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
}

结果

enter image description here

网友评论