导航栏图像上的纵横比填充? [英] Aspect Fill on navigation bar image?
问题描述
我希望为导航控制器的背景图像实现与 imageView 的纵横填充相同的外观.
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 显然给了我一个令人讨厌的拉伸外观.我不在乎图像是否在顶部/底部被截断,我只需要将其放入全尺寸的导航栏并显示内容.
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.
我不能只将 nil 传递给调整大小模式.
I can't just pass in nil to the resizing mode.
还有
init(rawValue:))
调整大小模式的选项,但我在那里输入的任何数字都会导致背景中不再有图像.
option for the resizing mode, but any number I put in there results in there being no image in the background anymore.
有什么想法吗?
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
UINavigationBar.appearance().setBackgroundImage(imageToCache, forBarMetrics: .Default)
这篇关于导航栏图像上的纵横比填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!