I have this snippet of code used in viewDidLoad of a UIViewController. I\'va no errors. Images exists. I get the background but not the image. Image is a sort of logo.
func centeredNavBarImage (){
let navcontroller = navigationController!
let image = #imageLiteral(resourceName: "yourImage")
let imageView = UIImageView(image:image)
let bannerWidth = navcontroller.navigationItem.accessibilityFrame.size.width
let bannerHeight = navcontroller.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - image.size.width / 2
let bannerY = bannerHeight / 2 - image.size.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
This is a modified code from https://youtu.be/bLkuu_fmlsU
The bannerWidth takes into account a refresh button item I have on the right side of the navbar. Seems to work.
Run the function on ViewDidLoad