0

I currently have in image in a nav bar but it's overlapping the edge:overlap

Here is the code I use in viewDidLoad:

let logo = UIImage(named: "holy-grail-pub-logo-header-logo")
let imageView = UIImageView(image:logo)
imageView.contentMode = .ScaleAspectFit
self.navigationItem.titleView = imageView

I've tried setting the position manually using CGRECT but it wasn't changing anything:

let imageView = UIImageView(frame: CGRect(x: 0, y: -30, width: 100, height: 60))
    imageView.contentMode = .ScaleAspectFit
    let logo = UIImage(named: "holy-grail-pub-logo-header-logo")
    imageView.image = logo
    self.navigationItem.titleView = imageView

Any help will be appreciated!

SwiftyJD
  • 5,257
  • 7
  • 41
  • 92

1 Answers1

0

Basically I hope you need to adjust the edgeInsets for your imageView which we cannot do directly on UIImageView as of now.

Using the method given here:

I did convert it to Swift(for your ref):

extension UIImage {

class func imageWith(image: UIImage, scaledToSize: CGSize) -> UIImage {
        //UIGraphicsBeginImageContext(newSize);
        // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
        // Pass 1.0 to force exact pixel size.
        UIGraphicsBeginImageContextWithOptions(scaledToSize, false, 0.0)
        image.drawInRect(CGRectMake(0, 0, scaledToSize.width, scaledToSize.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
}

Using above method:

let image = UIImage.imageWith(UIImage(named: "holy-grail-pub-logo-header-logo")!, scaledToSize: CGSizeMake(80,80))
    let imageView = UIImageView()
    imageView.frame = CGRectMake(0, 0, 100, 100)
    imageView.contentMode = .ScaleAspectFit
    imageView.image = image

Now your insets become 100-80 i.e. 20. I guess this workaround would help you fixing your issue. Try this and let us know if it works.

Community
  • 1
  • 1
Santosh
  • 2,900
  • 1
  • 16
  • 16