2015-12-04 90 views

回答

1

我已通过添加导航栏下方的视图这样做过。

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) 
} 
+0

这个代码不工作,请编辑你的答案或删除它 –

1

斯威夫特3:

let shadowView = UIView(frame: navBar.frame) 
shadowView.backgroundColor = UIColor.white 
shadowView.layer.masksToBounds = false 
shadowView.layer.shadowColor = UIColor.lightGray.cgColor 
shadowView.layer.shadowOpacity = 0.8 
shadowView.layer.shadowOffset = CGSize(width: 0, height: 2.0) 
shadowView.layer.shadowRadius = 2 
view.addSubview(shadowView) 
1

,如果我没看错的,默认的导航栏已经带有了一层阴影。但是,如果你正在做一个自定义的与一个UIView,那么你应该这样做,使您的生活更轻松

yourView.clipsToBounds = false 
yourView.layer.shadowOffset.height = 5 //this is your offset 
yourView.layer.shadowOpacity = 0.25 //opacity of your shadow (recommended) 
11

不添加视图

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 
+0

我尝试此代码和其他代码也是问题是,当我使用此导航变成浅灰色的颜色。但我想用白色和按钮阴影导航。 – chetu