3

图像尺寸为 640 X 44,适用于 iPad Portrait。由于某种原因,它显示为图案图像而不是拉伸。

在此处输入图像描述

iOS 6

4

3 回答 3

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];
于 2013-11-14T19:47:41.097 回答
4

原因是 ipad 的分辨率是 1024x768。因此,图像的宽度应该是 768

于 2013-11-14T19:44:43.513 回答
1

一种方法是根据导航栏的宽度调整图像大小。

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!
    }
}
于 2017-11-02T05:58:26.877 回答