swiftappdelegatetitleview

Can't update titleView image in App Delegate (Swift)


I want a logo to be present throughout my app. My entire app has an embedded Navigation Controller so I figured that I could set this image in the app delegate like so:

let logo = UIImage(named: "logo")
        let imageView = UIImageView(image:logo)
        UINavigationBar.appearance().topItem?.titleView = imageView

But it doesn't appear to be working. Is there something that I am doing wrong?


Solution

  • You cannot achieve this using appearance() API.

    Instead, you either have to provide an image view for each controller's navigation item's title view, or you can subclass UINavigationBar to add an image view that will always be there, regardless of what is in the navigation item. Then set all your controllers' titles to nil/empty string.