UITabBar background image not showing properly

大憨熊 提交于 2020-06-02 11:14:38

问题


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

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

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

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?


回答1:


Just redraw the image:

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


来源:https://stackoverflow.com/questions/38603207/uitabbar-background-image-not-showing-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!