Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add shadow to Navigation Bar instead of default border

Tags:

ios

swift

swift3

I want to replace a default border in UINavigationBar with shadow. Is there any way to achieve this? I have tried using the same approach as with adding shadow to UIView but it increases a height of my NavigationBar.

like image 232
alexxjk Avatar asked Aug 05 '16 07:08

alexxjk


1 Answers

try this

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
 }

output as

enter image description here

like image 80
Anbu.Karthik Avatar answered Nov 18 '22 03:11

Anbu.Karthik