The Finnternet Like the Internet but with more me

Get Swifty: Add image to navigation bar title spot

When working with navigation bars, setting a text title is a cinch, but I wanted to set a logo instead. This ended up requiring a pretty short custom class.

Fixed Image Nav Item

Create a new Cocoa Touch file with the following code. Then apply this class to your navigation item.

class FixedImageNavigationItem: UINavigationItem {
    private let fixedImage : UIImage = UIImage(named: "logo.png")
    private let imageView : UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 37.5))

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
                imageView.contentMode = .scaleAspectFit
                imageView.image = fixedImage
             self.titleView = imageView
        }
}