Skip to content

Instantly share code, notes, and snippets.

@SAllen0400
Created January 23, 2017 04:26
Show Gist options
  • Save SAllen0400/4fc41db92d9956507eef71cc782d0f36 to your computer and use it in GitHub Desktop.
Save SAllen0400/4fc41db92d9956507eef71cc782d0f36 to your computer and use it in GitHub Desktop.
Show logo in iOS NavBar
// Swift 3
// The reason I'm passing a UINavigationController and UINavigationItem as parameters is so
// this method can be abstracted away from the view controller.
func showLogoInNavBar(_ navController: UINavigationController, navItem: UINavigationItem) {
let banner = UIImage(named: "logo-nav-bar")
let imageView = UIImageView(image: banner)
let bannerWidth = navController.navigationBar.frame.size.width
let bannerHeight = navController.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - banner!.size.width / 2
let bannerY = bannerHeight / 2 - banner!.size.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: 180, height: bannerHeight)
imageView.contentMode = UIViewContentMode.scaleAspectFit
navItem.titleView = imageView
}
// Call the funtion in viewDidLoad by passing in the navigationController and navigationItem
override func viewDidLoad() {
super.viewDidLoad()
showLogoInNavBar(self.navigationController!, navItem: self.navigationItem)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment