UIBarButtonItem图像正在拉伸,即使使用的图像是建议的20 x 20分



我正在尝试自定义导航栏,并且有两个栏按钮项需要显示图标。我刚刚在故事板中创建了条形按钮项目,并将每个项目的图像设置为图标。这两个图标的大小都是正确的png(20 x 20pt),但它们显示的比应该显示的要大得多,所以它们在导航栏上看起来是双向的。

除了必须在viewDidLoad方法中添加一些代码外,我在故事板中做所有的事情。我不确定这是否有影响,但它是:

//Make the navigation bar transparent
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
//Setup navigation bar with Avenir Next Regular font and white title color
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AvenirNext-Regular" size:17], NSFontAttributeName, nil]];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

我是一个编码新手,这是我在这里的第一个问题。我会张贴图片来说明这个问题,但我需要10个信誉点才能做到这一点。

提前感谢您的帮助!

您必须为不同的设备提供多种分辨率的图像。最简单的方法是使用Xcode中的xcassets文件夹并填写所有模板:正常大小,2x,3x。

如果你不想使用xcassets,你必须自己命名文件:

image.png
image@2x.png
image@3x.png

Xcode会自动选择正确的尺寸。

最新更新