UITabBar背景图像无法正确显示 [英] UITabBar background image not showing properly

查看:55
本文介绍了UITabBar背景图像无法正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了自定义的UITabBar类,并尝试设置背景图片.

I made custom UITabBar class and tried to set background image.

tabBar.backgroundImage = UIImage(named: "my_image")?.imageWithRenderingMode(.AlwaysOriginal)

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

I set the image file name to my_image@2x and image file is 640*98

我在iPhone6模拟器上运行,看来图像不够宽,就像在下面的示例中重复使用Google的"C"

I run on iPhone6 simulator and it seems the image is not wide enough like Google's "C" is repeated on sample below

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

Im I using wrong image size or is something else is wrong?

推荐答案

只需重绘图像:

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)
}

这篇关于UITabBar背景图像无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆