UIButton背景图像stretchableImageWithLeftCapWidth:topCapHeight问题
我有一个指定宽度为 200 的 UIButton
。其 autoresizingMask
属性设置为 UIViewAutoresizingFlexibleWidth
。 UIImage
应用于此按钮的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不是真的,但考虑到图像的大小,您应该重新阅读 leftCapWidth 属性。
可拉伸图像的工作方式是:您提供左帽,拉伸下一个像素,并且图像的右侧
width = (total width - left cap + 1)
保持不变。鉴于您将左上限设置为 10,并且原始图像为 400,您将告诉 iOS 图像的右侧不可拉伸的右侧为400-10-1=389px
。同样的事情也适用于垂直方向。检查您是否在名称上没有 @2x 的普通设备上使用 @2x 图像,或者 2x 版本是否没有恰好两倍的像素,或者存在大写/小写差异。您可以通过记录加载图像的大小来找到这一点。我不知道为什么你的图像的右侧被拉伸。鉴于您有
height=0
,如果按钮高度发生变化,整个图像可以垂直拉伸,但这不太可能导致仅右侧变形。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 is400-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.据我所知,您的代码没有任何问题,这应该会导致图像的右侧被裁剪。下面是我使用的确切代码,我知道它有效,可以产生您正在寻找的相同效果。
我的猜测是问题来自您正在使用的 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.
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.