xcode 导航栏背景图片太大

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11576966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:57:10  来源:igfitidea点击:

Navigation Bar Background Image too big

iosxcodeuinavigationcontrolleruinavigationbar

提问by Chris Lin

I am trying to change the background image for my navigation bar using setBackgroundImage: forBarMetrics:in my setting method.

我正在尝试使用setBackgroundImage: forBarMetrics:我的设置方法更改导航栏的背景图像。

Problem is the image doesn't fit onto the bar, looks like the image is stretched at least double the original size...

问题是图像不适合栏,看起来图像被拉伸至少是原始尺寸的两倍......

The image size is 320 x 44...

图像大小为320 x 44...

I tried rename image to @2x, but it didn't change a thing...

我尝试将图像重命名为@2x,但它没有改变任何事情......

I tried assign this image to navigationItem.titleViewbut the view got cut off on the edge...

我尝试将此图像分配给navigationItem.titleView但视图在边缘被切断...

does any one have hint how to set the image so that it won't get stretched?

有没有人提示如何设置图像以使其不会被拉伸?

UINavigationBar *naviBar = [self.navigationController navigationBar];
UIImage *img = [UIImage imageNamed:@"[email protected]"];
[naviBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];

Thanks in advance

提前致谢

回答by Chris Lin

ok i got it finally!

好的,我终于明白了!

problem: navigation bar takes images thats 320 x 44 pixels, since each point in iPhone is 2 pixels, if you are using points for navigation bar, the image that you use will look like its stretched 4 times (2x for y, and 2x for x coordinate).

问题:导航栏拍摄的图像为 320 x 44 像素,因为 iPhone 中的每个点都是 2 像素,如果您使用点作为导航栏,您使用的图像看起来会被拉伸 4 倍(y 为 2x,y 为 2x x 坐标)。

to solve it, simply add file extension @2x to your image file (e.g. [email protected]). You must rename it BEFORE you drag into Xcode! it will NOT work if you try to rename it in Xcode.

要解决它,只需将文件扩展名@2x 添加到您的图像文件(例如 [email protected])。在拖入 Xcode 之前,您必须重命名它!如果您尝试在 Xcode 中重命名它,它将不起作用。

ps. DO NOT include @2x in your code, just the file name like so [UIImage imageNamed: @"yourImgName.png"]

附:不要在你的代码中包含@2x,只是像这样的文件名 [UIImage imageNamed: @"yourImgName.png"]

hope this helps

希望这可以帮助

回答by Selkie

You should have two image. One is 320x44 pixles and name as NavBar02.png. Another is 640x88 pixels and name as [email protected].

你应该有两个图像。一种是 320x44 像素,名称为 NavBar02.png。另一个是 640x88 像素,名称为 [email protected]

iOS will select the suitable image based on device's resolution.

iOS 将根据设备的分辨率选择合适的图像。

回答by Ryan B.

for all icons, apple recommends the following. image sizes for navigation and tab bars.

对于所有图标,苹果建议如下。 导航和标签栏的图像大小。

To resize, open your image in Preview. go to

要调整大小,请在预览中打开您的图像。去

tools -> adjust size.

工具 -> 调整大小。

Hope that helps.

希望有帮助。