2013-11-14 99 views

回答

3

的原因是,iPad的分辨率是1024x768.Thus,圖像的寬度應爲768

+0

哦!男人你是對的!爲什麼我認爲640分heheh :)現在已經修好了!謝謝! –

19

試試下面的伸展你的圖像:

// load the background image navbar.png 
UIImage *imageNavBar = [UIImage imageNamed:@"navbar"]; 

// set the image as stretchable and set into navbar globally 
imageNavBar = [imageNavBar stretchableImageWithLeftCapWidth:0 topCapHeight:0]; 
[[UINavigationBar appearance] setBackgroundImage:imageNavBar forBarMetrics:UIBarMetricsDefault]; 
+0

如果背景圖像具有透明部分,這也完全可行。即使您的背景沒有超出導航欄寬度,您也可以看到它通過透明部件重複出現。偉大的解決方案... –

+0

你拯救我的一天 – mychar

0

一種方法是根據導航欄的寬度調整圖像的大小。

let navBackgroundImage:UIImage! = UIImage(named: "top_header_iPhone") 
let navimg = navBackgroundImage.resizeImageWith(newSize: CGSize(width: UIScreen.main.bounds.size.width, height: navBackgroundImage.size.height)) 

UINavigationBar .appearance().setBackgroundImage(navimg, for:.default) 

UINavigationBar.appearance().titleTextAttributes = [ NSFontAttributeName: UIFont(name: "FightingSpiritturbo", size: 23)!] 

而對於調整圖像的功能是:

extension UIImage{ 

    func resizeImageWith(newSize: CGSize) -> UIImage { 

     let horizontalRatio = newSize.width/size.width 
     let verticalRatio = newSize.height/size.height 

     let ratio = max(horizontalRatio, verticalRatio) 
     let newSize = CGSize(width: size.width * ratio, height: size.height * ratio) 
     UIGraphicsBeginImageContextWithOptions(newSize, true, 0) 
     draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize)) 
     let newImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return newImage! 
    } 
}