0

I have the following code for a navigation bar that sets the word Title for the title but want it to be my logo instead.

     navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 44));

    navigationbar.barTintColor = UIColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1.0);

    let title = UINavigationItem(title: "Title");

    navigationbar.setItems([title], animated: false);

    self.view.addSubview(navigationbar);

Also what size in pixels should it be for @1x, @2x and @3x if I want a square logo? The logo is also my app icon.

cdub
  • 24,555
  • 57
  • 174
  • 303
  • Out of curiosity, why aren't you setting it in a storyboard? And why on *earth* would you manually set nav items in the navigation bar? – NRitH Sep 13 '18 at 04:46
  • Cuz I want to learn the roots of the sdk – cdub Sep 13 '18 at 04:59
  • You need title and image both ? or only image in navigationbar ? – Hardik Thakkar Sep 13 '18 at 04:59
  • Only image in the center (no title) – cdub Sep 13 '18 at 05:00
  • 1
    You shouldn't add your App icon to your app interface. If you do, it won't be published. For more information read: [HIG](https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/app-icon/) – Matthew Xavier Jacome Sep 13 '18 at 05:01
  • i don't know about you programatically code for navigationbar but you can add by this code........ *navigationController?.navigationItem.titleView = UIImageView(image: UIImage(named: "logo"))* try this one – Hardik Thakkar Sep 13 '18 at 05:05
  • It isn't per se my app icon but my app's name in a logo like the Netflix app – cdub Sep 13 '18 at 05:08
  • @hardik how do you make the width and height of the imageview with that code? thanks – cdub Sep 13 '18 at 05:27
  • About height/width of i don't have any idea. put image in assets folder and try to see output of existing image you have. – Hardik Thakkar Sep 13 '18 at 05:32

2 Answers2

0

Not entirely sure if this is what you are trying to achieve but there are a few suggestions already on stack.

Here is a popular one:

let logo = UIImage(named: "logo") // Or logo.png if you haven't added it to assets
let imageView = UIImageView(frame: CGRect(x:0, y:0, width:50.0, height:50.0))
imageView.image = logo
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView

Alternatively could use this method:

func addNavBarImage() {

    let navController = navigationController!

    let image = UIImage(named: "logo.png") //Your logo url here
    let imageView = UIImageView(image: image)

    let bannerWidth = navController.navigationBar.frame.size.width
    let bannerHeight = navController.navigationBar.frame.size.height

    let bannerX = bannerWidth / 2 - (image?.size.width)! / 2
    let bannerY = bannerHeight / 2 - (image?.size.height)! / 2

    imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
    imageView.contentMode = .scaleAspectFit

    navigationItem.titleView = imageView
}

Answer from this question.

App Dev Guy
  • 5,396
  • 4
  • 31
  • 54
0

Try this one

// top-logo = your image name 
if let logo = UIImage(named: "top-logo"){
    let imageView = UIImageView(image:logo)
    self.navigationItem.titleView = imageView
}

Output :

enter image description here

Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51