在swift中将阴影应用于导航栏

Saj*_*jad 10 ios swift

在我的设计导航的应用程序有很好的阴影,我想在我的代码中应用.如何才能创造出这种效果?

在此输入图像描述

Far*_*ric 39

没有添加视图

self.navigationController?.navigationBar.layer.masksToBounds = false
self.navigationController?.navigationBar.layer.shadowColor = UIColor.lightGray.cgColor
self.navigationController?.navigationBar.layer.shadowOpacity = 0.8
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 2.0)
self.navigationController?.navigationBar.layer.shadowRadius = 2
Run Code Online (Sandbox Code Playgroud)


pro*_*lfe 5

我之前通过在导航栏下方添加视图来完成此操作。

func addShadowToBar() {
    let shadowView = UIView(frame: self.navigationController!.navigationBar.frame)
    shadowView.backgroundColor = UIColor.whiteColor()
    shadowView.layer.masksToBounds = false
    shadowView.layer.shadowOpacity = 0.4 // your opacity
    shadowView.layer.shadowOffset = CGSize(width: 0, height: 2) // your offset
    shadowView.layer.shadowRadius =  4 //your radius
    self.view.addSubview(shadowView)
}
Run Code Online (Sandbox Code Playgroud)