-3

I am trying to resize my image so it fits to navigation title. I give it height and weight but it still looks huge like this

and here is my code:

 override func viewDidLoad() {
       super.viewDidLoad()


    let image = UIImage(named: "rollyiha copyi")
    let imageview = UIImageView(image: image)
    imageview.frame.size.width = 58

    imageview.frame.size.height = 33
    imageview.frame.origin = CGPoint(x: 2, y: 8)

    imageview.sizeToFit()
    navcik.titleView = imageview
}

And when I edit the picture to smaller size manually, it loses resolution. How to make it size 58 : 33 with code ?

JackLappa
  • 99
  • 11

2 Answers2

0

I think your imageViews size is determined by the intrinsicContentSize property of the imageView.

You could try imageview.invalidateIntrinsicContentSize()

0
func addNavBarImage() {
    let navController = navigationController!

    let image = UIImage(named: "logo-signIn6.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
}
Sasha
  • 104
  • 8
  • OK, this is right, one more problem, now the image is too much to the left. How to move it a little bit to the right from x axis? – JackLappa Feb 02 '19 at 23:31
  • This still doesn't change the image view size . Only the scaleAspectFit will make the image fits into the image view without scaling. – Teja Nandamuri Feb 02 '19 at 23:39