使用 resizableImageWithCapInsets: image for button 仅适用于状态集,其他状态显示“间隙"; [英] Using resizableImageWithCapInsets: image for button only works for the state set, other states show a "gap"

查看:19
本文介绍了使用 resizableImageWithCapInsets: image for button 仅适用于状态集,其他状态显示“间隙";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 resizableImageWithCapInsets: 为 UIButton 创建图像时,只有正常状态(使用 setBackgroundImage:forState: 设置图像的状态)有效.所有其他状态显示一个间隙而不是绘制的图像.UIButton 表示,如果没有为特定状态设置图像,则正常状态图像将与禁用和选定状态的叠加层一起使用.

When using resizableImageWithCapInsets: to create an image for a UIButton only the normal state (the state set the image with using setBackgroundImage:forState:) works. All other states show a gap instead of the drawn image. UIButton says that if no image is set for a particular state, the normal state image will be used with an overlay for disabled and selected states.

这是正常状态:

这里是选中状态:

这是源图像:

它显然使用了我提供的可调整大小的图像,但该图像没有绘制调整大小的区域.(可以看到左右边缘,但是中间要拉伸的区域没有画出来).

It clearly is using the resizable image I provided, but the image is not drawing the resized area. (You can see the left and right edges but the middle area that is to be stretched just isn't drawn).

有趣的是,stretchableImageWithLeftCapWidth:topCapHeight: 确实有效.现在这是 iOS 5 中已弃用的方法,但由于新 API 中显示了差距,我可能无法使用它.

Interestingly, stretchableImageWithLeftCapWidth:topCapHeight: does work. Now this is a deprecated method in iOS 5, but with the gap being shown in the new API, I may be stuck using it.

我确实认识到我可以为每个状态提供更多图像,但这违背了我试图实现的减少内存占用的目的,并且增加了对我希望避免的图形设计器的额外依赖.

I do recognize that I can provide more images for each state but that defeats the purpose I'm trying to achieve of reducing memory footprint plus adds extra dependency on my graphics designer which I'd like to avoid.

// This is the gist of the code being used
UIImage* image = [UIImage imageNamed:@"button.png"];
UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2);
image = [image resizableImageWithCapInsets:insets];
[self.button setBackgroundImage:image forState:UIControlStateNormal];
// Even doing the following results in the same behaviour
[self.button setBackgroundImage:image forState:UIControlStateSelected];

推荐答案

您没有为图像封顶正确创建插图.我已经复制了您的问题并使用正确的插图进行了更正.

You aren't creating your insets properly for the image capping. I've reproduced your issue and corrected it by using the correct insets.

使用您当前的代码,您将创建图像高度和宽度一半的上限 - 这会为您留下一个 0x0 像素的可拉伸"区域 - 所以中间什么也没有.

With your current code, you are creating caps of half of the image height and width - this leaves you with a "stretchable" area of 0x0 pixels - so you get nothing in the middle.

为什么这在按钮的正常状态下没有显示为错误我不确定 - 如果您不提供可拉伸图像,UIButton 可能内置了一些优化来修复问题或自动拉伸,这不适用于其他州.

Why this isn't showing up as wrong in the normal state of the button I'm not sure - perhaps there is some optimisation built in to UIButton to fix things or auto-strectch if you don't supply a stretchable image, and this is not applied to the other states.

帽子应该定义不能拉伸的图像区域.对于您的 button.png 图像,左侧和右侧为 6 个像素,顶部和底部为 16 个像素.这不是很标准,你应该告诉你的图形设计师(至少对于最常见的左右拉伸)你应该只在中心有一个 1px 的区域,但这不会影响结果.如果您确实有一个 1px 可拉伸区域,那么您可以通过从图像大小派生大写来标准化您的代码,就像您在问题中尝试做的那样(每个大写都是 (image.size.height - 1)/2 用于顶部/底部,相同但具有左侧/右侧宽度).

The caps are supposed to define the area of the image that must not be stretched. In the case of your button.png image, this is 6 pixels on the left and right sides, and 16 pixels in from the top and bottom. This isn't quite standard, you should tell your graphics designer that (at least for left-right which is the most common stretching) you should only have a 1px area in the centre, however this does not affect the outcome. If you do have a 1px stretchable area then you can standardise your code by deriving the caps from the image size as you have tried to do in your question (each cap is then (image.size.height - 1) / 2 for top/bottom, same but with width for left/right).

要在您的按钮上获得正确的图像,请使用以下代码创建可拉伸图像:

To get the correct images on your button, use the following code for creating the stretchable image:

UIEdgeInsets insets = UIEdgeInsetsMake(16, 6, 16, 6);
image = [image resizableImageWithCapInsets:insets];

这篇关于使用 resizableImageWithCapInsets: image for button 仅适用于状态集,其他状态显示“间隙";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆