如何拉伸显示UITabbar的backgroundImage

2,193 阅读1分钟

设备:iOS12.2 iPhone XR xcode10.2.1

遇到个问题记录一下: UITabbar的backgroundImage默认平铺显示,我试图让它拉伸显示

一开始我试图查下文档Stack Overflow: 如果你想拉伸看下UIImage.Resizing。👌 看起来很容易

let img = UIImage(named:"backgroundTabbar")?.resizableImage(withCapInsets: .zero, resizingMode: .stretch)
homeVC?.tabBar.backgroundImage = img

看起来并不ok🤔 试了多种参数都不行 受不了 咱直接看看他到底啥意思

/* The background image will be tiled to fit, even if it was not created via the UIImage resizableImage methods.
     */
    @available(iOS 5.0, *)
    open var backgroundImage: UIImage?

好的 注释说了 就是平铺,你想咋的? resizableImage也不行🙃

不让就不让 也行 至少知道问题出在哪了 继续google 找一个11年的帖子 说用stretchableImage(withLeftCapWidth:topCapHeight:)这个能解决 抱着死马当活马医 实践是检验真理唯一标准的心态试了一下 还真成了!

let img = UIImage(imageLiteralResourceName: "backgroundTabbar").stretchableImage(withLeftCapWidth: 0, topCapHeight: 0)
homeVC?.tabBar.backgroundImage = img

Creates and returns a new image object with the specified cap values.

Deprecated

Deprecated. Use the resizableImage(withCapInsets:) instead, specifying cap insets such that the interior is a 1x1 area.

文档看了一眼 ,这个处理图片的方法iOS5被弃用了,但我在iOS12用完全不报警。。咱也不知道 咱也不敢问😂 以上