如何将阴影添加到导航栏而不是默认边框

时间:2016-08-05 07:51:15

标签: ios swift swift3

我想用阴影替换UINavigationBar中的默认边框。有没有办法实现这个目标?我尝试使用与向UIView添加阴影相同的方法,但它增加了我的NavigationBar的高度。

1 个答案:

答案 0 :(得分:22)

试试这个

self.navigationController.navigationBar.layer.shadowColor = UIColor.blackColor().CGColor
self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(2.0, 2.0)
self.navigationController.navigationBar.layer.shadowRadius = 4.0
self.navigationController.navigationBar.layer.shadowOpacity = 1.0

Swift 3

 override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
    self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
    self.navigationController?.navigationBar.layer.shadowRadius = 4.0
    self.navigationController?.navigationBar.layer.shadowOpacity = 1.0
    self.navigationController?.navigationBar.layer.masksToBounds = false
 }

输出为

enter image description here