UITabBar背景图像无法正常显示

时间:2016-07-27 03:28:07

标签: ios swift image uitabbar

我制作了自定义UITabBar类并试图设置背景图像。

{{1}}

我将图像文件名设置为my_image @ 2x,图像文件为640 * 98

我在iPhone6模拟器上运行,似乎图像不够宽广 Google的“C”会在下面的示例中重复

enter image description here

我是使用错误的图像尺寸还是其他错误?

1 个答案:

答案 0 :(得分:2)

只需重新绘制图片:

var image = UIImage(named: "my_image")
if let image = image {
    var centerImage: Bool = false
    var resizeImage: UIImage?
    let size = CGSize(width: UIScreen.mainScreen().bounds.size.width, height: 98)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    if centerImage {
        //if you want to center image, use this code
        image.drawInRect(CGRect(origin: CGPoint(x: (size.width-image.size.width)/2, y: 0), size: image.size))
    }
    else {
        //stretch image
        image.drawInRect(CGRect(origin: CGPoint.zero, size: size))
    }
    resizeImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    tabBar.backgroundImage = resizeImage.imageWithRenderingMode(.AlwaysOriginal)
}