iphone - UIButton backgroundImage stretchableImageWithLeftCapWidth :topCapHeight issue

标签 iphone objective-c uikit uibutton uiimage

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

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

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

提前致谢!

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

已解决! 如果您的图像名为“myImage.png”并且是视网膜版本,则只需将其命名为“myImage@2x.png”

最佳答案

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?

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

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

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

关于iphone - UIButton backgroundImage stretchableImageWithLeftCapWidth :topCapHeight issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5766437/

相关文章:

iphone - 具有自定义类型的 UIButton 似乎无法正常工作

iphone - 如何限制 UINavigationBar 中的 rightBarButtonItem 大小?

ios - 如何在 iOS 中为 3 个音频文件显示相同的进度条

iphone - 修改 Storyboard 后 Objective-C 释放僵尸对象

iphone - 发现 iOS 二进制文件的 iOS 版本

iphone - 如何制作我正在编写的 2 个应用程序交换信息?

objective-c - Objective-C 的加密库

firebase - 与守护程序的连接无效

ios - 为什么 touchesForView : returns only 1 touch?

ios - 如何在 SwiftUI 中使用 UIViewController 和 UIView?