I'm wanting to achieve the same look as an imageView's aspect fill for my navigation controller's background image.
Currently I'm using this to setup the image:
UINavigationBar.appearance().setBackgroundImage(imageToCache?.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: UIImageResizingMode.stretch), for: .default)
UIImageResizingMode.stretch obviously gives me a nasty stretched look. I don't care if the image is getting cut off on the top/bottom, I'm just needing it to be put into the nav bar full sized and what shows shows.
I can't just pass in nil to the resizing mode.
There's also the
init(rawValue:))
option for the resizing mode, but any number I put in there results in there being no image in the background anymore.
Any ideas?
edit:
let picture = passedPerson.pictureURL!
if let imageFromCache = MainPageVC.imageCache.object(forKey: picture as String as NSString) {
var titleView = UIImageView()
titleView.contentMode = .scaleAspectFit
titleView.image = imageFromCache
self.navigationItem.titleView = titleView } else {
print(picture)
var requested = URLRequest(url: URL(string: picture)!)
URLSession.shared.dataTask(with: requested) {data, response, err in
if err != nil {
print(err?.localizedDescription)
} else {
DispatchQueue.main.async {
let imageToCache = UIImage(data: data!)
MainPageVC.imageCache.setObject(imageToCache!, forKey: picture as String as NSString)
var titleView = UIImageView()
titleView.contentMode = .scaleAspectFit
titleView.image = imageToCache
self.navigationItem.titleView = titleView
// UINavigationBar.appearance().setBackgroundImage(imageToCache?.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: UIImageResizingMode.stretch), for: .default)
}
}
}.resume()
The above results in no picture showing up.
try with this . hope it you will work