0

Currently this is what I have:

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNavigationBar()
}

func setNavigationBar() {
    let screenSize: CGRect = UIScreen.main.bounds
    let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: 44))
    let navItem = UINavigationItem(title: "")
    let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: nil, action: #selector(done))
    navItem.rightBarButtonItem = doneItem
    navBar.setItems([navItem], animated: false)
    self.view.addSubview(navBar)
}

@objc func done() { // remove @objc for Swift 3

The issue is that the bars title seems to be off center and the bar is very thin.

How can it be made a little thicker and with an image instead of the title?

UPDATE:

The first suggestion seemed to do nothing. Why is that? This is the first view controller.

Full code:

import UIKit
import Firebase
import FirebaseAuth


class LoginViewController: UIViewController {
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!

@IBOutlet weak var gifView: UIImageView!

private var ref: DatabaseReference!

override func viewDidLoad() {


    let logo = UIImage(named: "q-small.png")
    let imageView = UIImageView(image:logo)
    self.navigationItem.titleView = imageView

    gifView.loadGif(name: "truck-animation")
    super.viewDidLoad()

    ref = Database.database().reference()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
  • Possible duplicate of [How to put an image as the navigation bar title](https://stackoverflow.com/questions/865176/how-to-put-an-image-as-the-navigation-bar-title) – Rishil Patel Jan 28 '19 at 05:47

2 Answers2

0

This is similar to Swift Navigation Bar Image Title.

Simply use this code:

let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
0

It seems like you are creating UINavigationBar programmatically and i am also assuming that you are creating app using storyboard.

self.navigationItem returns navigation items of your ViewController. But as per your code you must have created just UINavigationBar. So to get the title view you need UINavigationController of your ViewController.

If this is your first view of app You can add that by embedding your ViewController into Navigation Controller from Storyboard, then you don't have to add navigation bar programmatically.

After that you can use this code in viewDidLoad().

let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Nilay
  • 327
  • 3
  • 9