Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding System Image to navigation bar programmatically

I'm trying to move from storyboards to coding programmatically. I'm trying to add a barbuttonitem to my navigation bar. However, I don't want to use my own images, I want to use the default system images, such as "magnifyingglass.circle".

enter image description here

Here is my code:

let button = UIBarButtonItem.init(image: UIImage(contentsOfFile: "magnifyingglass.circle") , style: .plain, target: nil, action: nil)
navigationItem.leftBarButtonItem = button

However, nothing is added to my navigation bar.

like image 632
Dan.code Avatar asked Aug 23 '26 07:08

Dan.code


1 Answers

You are using the wrong UIImage initializer. The one you are trying to use requires a full path to an image file. You want UIImage(systemName:).

let button = UIBarButtonItem(image: UIImage(systemName: "magnifyingglass.circle") , style: .plain, target: nil, action: nil)
like image 71
rmaddy Avatar answered Aug 24 '26 23:08

rmaddy