UIButton背景图像stretchableImageWithLeftCapWidth:topCapHeight问题

发布于 2024-11-02 21:57:28 字数 867 浏览 0 评论 0原文

我有一个指定宽度为 200 的 UIButton。其 autoresizingMask 属性设置为 UIViewAutoresizingFlexibleWidthUIImage 应用于此按钮的 backgroundImage 属性。此 UIimage 定义如下:

[[UIImage imageNamed:@"buttonimage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0]

buttonimage.png 的宽度为 400px(由于视网膜分辨率的原因,是按钮宽度的 2 倍),表示一个圆角矩形。在肖像模式下,一切都很好。一旦用户旋转设备并且 iPhone 进入横向模式,UIButton 就会拉伸。由于这种行为,图像的左圆角边框保持不变 (stretchableImageWithLeftCapWidth:),但右角也被拉伸。是否有我忘记设置的任何属性可以确保仅拉伸一个指定的像素(例如第十个像素),而其他任何像素都保留其尺寸?

提前致谢!

编辑:如果我使用较小的图像,该图像已经在纵向模式下拉伸,则两个边框似乎都被扩展。

解决了! 如果您的图像名为“myImage.png”并且是视网膜版本,则只需将其命名为“ [电子邮件受保护]

I have a UIButton with a specified width of 200. Its autoresizingMask property is set to UIViewAutoresizingFlexibleWidth. A UIImage is applied to this button's backgroundImage property. This UIimage is defined as follows:

[[UIImage imageNamed:@"buttonimage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0]

buttonimage.png has a width of 400px (2x width of button, because of retina resolution) and represents a rounded rectangle. In portrait mode, everything is fine. As soon as the user rotates the device and the iPhone enters landscape mode, the UIButton is stretched. Due to this behaviour, the left rounded border of the image stays the same (stretchableImageWithLeftCapWidth:) but the right corners are also stretched. Is there any property I forgot to set that ensures only one specified pixel (e.g. the tenth one) is stretched and anything else retains it dimensions?

Thanks in advance!

Edit: If I use a smaller image, that is already stretched in portrait mode, both borders seem to be expanded.

Solved!
If your image is called "myImage.png" and it is the retina version, just call it "[email protected]"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷你仙 2024-11-09 21:57:28

有没有我忘记设置的属性
确保只有一个指定的像素
(例如第十个)被拉伸并且
还有什么可以保留它的尺寸吗?

不是真的,但考虑到图像的大小,您应该重新阅读 leftCapWidth 属性。

可拉伸图像的工作方式是:您提供左帽,拉伸下一个像素,并且图像的右侧 width = (total width - left cap + 1) 保持不变。鉴于您将左上限设置为 10,并且原始图像为 400,您将告诉 iOS 图像的右侧不可拉伸的右侧为 400-10-1=389px。同样的事情也适用于垂直方向。检查您是否在名称上没有 @2x 的普通设备上使用 @2x 图像,或者 2x 版本是否没有恰好两倍的像素,或者存在大写/小写差异。您可以通过记录加载图像的大小来找到这一点。

我不知道为什么你的图像的右侧被拉伸。鉴于您有 height=0,如果按钮高度发生变化,整个图像可以垂直拉伸,但这不太可能导致仅右侧变形。

Is there any property I forgot to set
that ensures only one specified pixel
(e.g. the tenth one) is stretched and
anything else retains it dimensions?

Not really, but given the size of your images, you should re-read the leftCapWidth property.

The way stretchable images work is: you provide the left cap, the next pixel is stretched, and the right side of the image with width = (total width - left cap + 1) stays the same. Given that you are setting left cap to 10, and the original image is 400, you are telling iOS that the right non stretchable right side of your image is 400-10-1=389px. The same thing applies vertically. Check if you are using @2x images on a normal device without a @2x on its name, or if the 2x versions don't have exactly twice the pixels, or there is an uppercase/lowercase difference. You can find this out nslogging the size of the image loaded.

I don't know why the right side of your image is stretched. Given that you have height=0, the whole image can be stretched vertically if the button height changes, but it's unlikely that this causes a distortion of only the right side.

飘然心甜 2024-11-09 21:57:28

据我所知,您的代码没有任何问题,这应该会导致图像的右侧被裁剪。下面是我使用的确切代码,我知道它有效,可以产生您正在寻找的相同效果。

UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 245, 51)];
button.autoresizingMask=UIViewAutoresizingFlexibleWidth;
[button setBackgroundImage:[[UIImage imageNamed:@"userbubble.png"] stretchableImageWithLeftCapWidth:40 topCapHeight:0] forState:UIControlStateNormal];
[self.view addSubview:button];

我的猜测是问题来自您正在使用的 png,或者可能来自项目中的“压缩 PNG 文件”设置。另外,由于您使用的图像太大,请尝试将左端盖进一步拉出,例如 40 或 50 像素。

There is nothing wrong with your code as far as I can tell, that should be causing the right side of the image to be cropped. Below is the exact code I use, which I know works, to produce the same effect you're looking for.

UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 245, 51)];
button.autoresizingMask=UIViewAutoresizingFlexibleWidth;
[button setBackgroundImage:[[UIImage imageNamed:@"userbubble.png"] stretchableImageWithLeftCapWidth:40 topCapHeight:0] forState:UIControlStateNormal];
[self.view addSubview:button];

My guess is that the problem is coming from the png you're using, or possibly from the Compress PNG Files setting in your project. Also, since the image you are using is so big, try putting the left cap out further, say 40 or 50 pixels in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文