2016-07-27 242 views
0

我製作了自定義的UITabBar類,並試圖設置背景圖像。UITabBar背景圖像顯示不正確

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

我設置圖像文件名my_image @ 2x和圖像文件是640 * 98

我在iPhone6模擬器上運行,它似乎像不夠寬,像重複 谷歌的「C」使用錯誤的圖像尺寸或低於

enter image description here

林樣我是別的東西是錯誤的?

回答

1

僅僅重繪圖像:

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

這工作,謝謝! – Bigair