AnaMM
AnaMM

Reputation: 317

How to set an image as title in all navigation bars in swift?

I need to know if it is possible to add an image as title on all navigation controllers, this mean, that every controller that had a navigation bar had the same image title.

I used the next code to set an image as title in the navigation bar, but I have to set it on every viewController, the idea its for example set it on AppDelegate as UINavigationBar.appereance() or similar.

let imageView         = UIImageView(image: UIImage(named: "my_image.png")!)
imageView.contentMode = .scaleAspectFit

let titleView         = UIView(frame: CGRect(x: 0, y: 0, width: 107, height: 30))
 imageView.frame       = titleView.bounds
titleView.addSubview(imageView) 

self.navigationItem.titleView = titleView

So the idea is to set this image as the default title for every controller, that way I will just need to configure it one time.

It is that posible? If not, What possible solution do you think that I can apply ?

Upvotes: 0

Views: 427

Answers (2)

Vijay Kahar
Vijay Kahar

Reputation: 1076

Please try below code,

let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView

if this not works please share the screenshot.

Upvotes: 0

matt
matt

Reputation: 536046

The title view is a property of each view controller, not the navigation bar or navigation controller.

So start with a UIViewController subclass whose navigation item has this title view, and make all your other view controllers be subclasses of that.

Upvotes: 1

Related Questions