Last active
February 16, 2018 21:00
-
-
Save Jered/28aaeb88d77fade1ad34691956dda81f to your computer and use it in GitHub Desktop.
Replace Navigation Item Text Title with an Image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Logo in place of title function (Swift 4) | |
// call this from viewDidLoad() | |
private func setSwooshTitle() { | |
// set the navigation title to a bundled image | |
let logoWidth = 60 | |
let logoHeight = 44 // this is actually the height of the navbar | |
let bundle = Bundle(for: type(of: self)) // get the asset bundle | |
let logo = UIImage(named: "icons.logo", in: bundle, compatibleWith: self.traitCollection)! | |
let imageView = UIImageView(frame: CGRect(x: -logoWidth/2, y: -logoHeight/2, width: logoWidth, height: logoHeight)) // set the bounds and positioning for the logo | |
let titleView = UIView() // wrapping in a UIView fixes a scaling problem | |
imageView.image = logo | |
imageView.contentMode = .scaleAspectFit // scale to fit inside frame proportionately | |
titleView.addSubview(imageView) | |
// set the navigationItem's titleView to our new view | |
self.navigationItem.titleView = titleView | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment