Skip to content

Instantly share code, notes, and snippets.

@rehannali
Created November 7, 2022 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rehannali/c6e3743793243e25dc0323c3f3ad36f7 to your computer and use it in GitHub Desktop.
Save rehannali/c6e3743793243e25dc0323c3f3ad36f7 to your computer and use it in GitHub Desktop.
Set Image in Navigation Bar
public extension UIViewController {
func setTitleImage(_ image: UIImage) {
let imageView = UIImageView(image: image)
let width = navigationController?.navigationBar.frame.size.width ?? 0
let height = navigationController?.navigationBar.frame.size.height ?? 0
let axisX = width / 2 - image.size.width / 2
let axisY = height / 2 - image.size.height / 2
imageView.frame = CGRect(x: axisX, y: axisY, width: width, height: height)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment