为 UIBarButtonItem 设置图像 - 图像拉伸
当我尝试使用 UIBarButtonItem 的“initWithImage”来初始化导航栏自定义图像时,它会被冲破并拉伸到黑色导航栏上。这就是我创建它的方式:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gear.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showSetting:)];
它看起来像这样:
知道这是否是问题图像?我是从我买的一组图标中得到的。
When I try to use UIBarButtonItem's "initWithImage" to initialize a navigation bar custom image, it comes out washed-up and stretched against a black navigation bar. This is how I create it:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gear.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showSetting:)];
Here is what it looks like:
Any idea if it's a problem with the image? I got it from a set of icons I bought.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
执行此操作的最佳方法是创建一个按钮,设置其背景图像并设置其操作。然后可以使用此按钮作为自定义视图来创建 UIBarButtonItem。这是我的示例代码:
The best way to do this is to create a button, set its background image, and set its action. Then a UIBarButtonItem can be created using this button as the custom view. Here's my example code:
栏按钮项目上显示的图像是从源图像“派生”的(它在渲染中仅使用 Alpha 通道值,但在图像中一切看起来都正常)。它可能只是尺寸不正确 - 您可能需要打开图像文件并将其裁剪为正确的尺寸。
您还可以尝试查看是否可以使用设置 imageInsets 属性(由 UIBarButtonItem 从 UIBarItem 继承)来调整大小阻止它被拉伸。
Doco 在栏项目图像上说明了以下内容:
栏上显示的图像源自此图像。如果该图像太大而无法放在条上,则会缩放以适合该图像。通常,工具栏和导航栏图像的大小为 20 x 20 点。
The displayed images on bar button items are 'derived' from the source image (it uses only the alpha channel values in rendering but that all looks ok in your image). Its possibly just not the right size - you might need to open the image file and crop it to the right size.
You could also try looking at whether setting the imageInsets property (inherited by UIBarButtonItem from UIBarItem) can be used to adjust the size in a way to stop it getting stretched.
Doco on the bar item images says the following:
The images displayed on the bar are derived from this image. If this image is too large to fit on the bar, it is scaled to fit. Typically, the size of a toolbar and navigation bar image is 20 x 20 points.
我知道这个问题已经有一个勾选的答案。但我今天遇到了这个问题,并认为无论如何我都会提供我的答案。上面勾选的答案确实对我有帮助,但也需要一些额外的实验才能弄清楚到底发生了什么。
按钮图像仅在 x 轴而非 y 轴上缩小。这是因为它对于按钮来说太高了,所以它会缩小以适应。但它并没有按比例缩小。仅在垂直方向上。所以显得很拉长。它实际上并没有被拉伸——这意味着它被加宽了。相反,高度缩小了。我认为了解差异对于理解为什么会发生以及如何解决它很重要。
我做了与OP相同的事情。考虑到我支持视网膜,我将图标设置为 40x40。我的是一个带有 Alpha 通道的绿色复选标记。它用空白像素填充为 40x40。该应用程序调整了它的大小以适合按钮的可用高度。但宽度保持不变。所以它变成了 40x30 或 40x20 范围内的某个地方。我认为按钮可以处理 30 高的图标,但恕我直言,对于盒子来说有点太大了。
OP 将按钮缩小到 30x30,这样它就不会再被压扁了。但这不是最好的解决方案。因为当你这样做时,它实际上并不是一个视网膜按钮。它先缩小,然后又在视网膜上放大。
正确的答案是用 @2x 命名 40 像素高的版本,然后制作半尺寸(20 像素高)的版本并保存,不带 @2x。宽度可以是任意的。然后使用 imageNamed: 加载,而不指定@2x。它将针对视网膜或非视网膜设备使用适当的 png。
接下来发生在我身上的事情是按钮框架太小了。因此,我提高了 psd 中的画布大小,将 png 填充到 80 宽,使按钮稍宽且更易于点击。
I know this question already has a checkmarked anser. But I ran in to this today and thought I would offer up my answer anyway. The check marked answer above did help me, but it also took some extra experimenting to figure out what was really happening.
The button image is being shrunk only in the x axis and not the y. this is because it is too tall for the button and it shrinks it down to fit. But it doesn't shrink it proportionally. Only on the vertical. So it appears stretched. It's not actually stretched - which implies a widening of it. Instead the height is shrunk. Knowing the difference I think is important to understanding why it's happening and how to fix it.
I did the same thing the OP did. Thinking that I am supporting retina I made my icon 40x40. Mine was a green checkmark with an alpha channel. It was padded with blank pixels to be 40x40. The app resized it to fit within the button's available height. But the width stayed the same. So it became somewhere in the range of 40x30 or 40x20. I think the button can handle an icon 30 high, but then it's a little too big for the box IMHO.
The OP reduced the button to 30x30 and that made it not squish any more. But that's not the best solution. Because it isn't actually a retina button when you do that. It's shrunk and then blown back up on the retina.
The correct answer is to name your 40 pixel tall version with the @2x and then make a half size (20 pixel tall) version and save it without the @2x. The width can be whatever. Then load with imageNamed: without specifying the @2x. It will use the appropriate png for a retina or non-retina device.
The next thing that happened to me was then the button frame was too small. So I upped my canvas size in psd to pad the png to 80 wide to make the button slightly wider and more tappable.
当我将背景图像设置为 < 时,我的 40x40 图像也遇到了同样的拉伸问题strong>leftBarButtonItem
但我的问题通过以下代码解决了
如果使用 UIBarButtonItemStyleBordered 则结果相同。
I got the same stretched issue for my 40x40 image when I set the background image for leftBarButtonItem
But my issue got resolved with the following code
and same result if UIBarButtonItemStyleBordered is used.
首先设置正确的图像尺寸:@1x = 22px,@2x = 44px @3x = 88px。
然后
或者
Set correct image size: @1x = 22px, @2x = 44px @3x = 88px firstly.
Then
or
对于那些在 iOS 11 中遇到过此工具栏项目拉伸问题的人来说,现在似乎需要 @2x 版本的图像来渲染其框架和/或边界。
因此,如果您有这样的代码,您要添加这样的自定义图像 UIBarButtonItem:
那么您将需要有一个 [电子邮件受保护] 即 80x80,即使您的 Tags.png 图片是 80x80。只需将 Tags.png 重命名为 [email protected] 即可将图像大小调整为 40x40就像 iOS 11 之前一样,无需更改代码,或者只需添加 [电子邮件受保护] 到您的项目。
For those who have come across this toolbar item stretching issue in iOS 11 specifically, it appears that the @2x version of your image is now required to render its frame and or bounds.
So if you have code like this where you're adding a custom image UIBarButtonItem like this:
Then you will need to have a [email protected] that's 80x80, even if your tags.png image is 80x80. Simply renaming tags.png to [email protected] would resize the image to 40x40 as it did pre iOS 11 without changing code, or just add [email protected] to your project.