创建像素完美的标准 UIBarButtonItem,其中包含自定义图像

发布于 2024-11-19 03:33:47 字数 669 浏览 2 评论 0 原文

在过去的几个小时里,我一直在为这个问题摸不着头脑。是否可以创建一个标准的UIBarButtonItem,其中内部有自定义图像?我想要的只是标准的蓝色按钮,但里面有一个代表偏好的图像(我自己创建的螺丝刀或齿轮)。我宁愿不使用 Photoshop 来完成此操作,因为我希望每个像素都是完美的且面向未来,以防 Apple 发布更高分辨率的 iOS 设备。

我在 GitHub 上发现了一个很棒的项目,其中包含 iOS 附带的每个图形元素,并且我提取了 Apple 本身在创建标准 UIBarButtonItems 时使用的图像。问题是,它无法以我所知道的方式调整到正确的尺寸。如果无法使用代码执行我想要的操作,那么也许有人可以告诉我如何使用此图像。

GitHub - UIKit-Artwork-Extractor

<跨度类=“__cf_email__” data-cfemail="e8bda1a6899e818f899c818786aa899aac8d8e899d849caa9d9c9c8786a8da90c698868f">[电子邮件受保护]

谢谢!

I've been scratching my head over this for the past couple of hours. Is it possible to create a standard UIBarButtonItem with a custom image inside? All I want is the standard blue button, but with an image that represents preferences (a screwdriver or cog wheel that I'll create myself) inside of it. I'd rather not use Photoshop to do this, since I'd like every single pixel to be perfect and future proof, in case Apple releases even higher resolution iOS devices.

I've found a wonderful project on GitHub that includes every graphic element shipped with iOS, and I've extracted what appears to be the image Apple itself uses when creating standard UIBarButtonItems. Problem is, it can't be resized to the proper dimensions in a way that I know of. In case it's not possible to do what I want with code, then maybe someone can tell me how to use this image.

GitHub - UIKit-Artwork-Extractor

[email protected]

Thanks!

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

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

发布评论

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

评论(2

醉生梦死 2024-11-26 03:33:47

只需以单一颜色创建图像(仅 Alpha 通道很重要)并使用 initWithImage:style:target:action: 具有 UIBarButtonItemStyleBordered 或 UIBarButtonItemStyleDone 样式。

如果您想要全彩图像,则必须使用链接的图像。秘密是通过以 Android 9 个补丁图像,而不是缩放整个图像。

Just create your image, in a single color (only the alpha channel is significant) and use initWithImage:style:target:action: with the UIBarButtonItemStyleBordered or UIBarButtonItemStyleDone style.

If you want a full-color image, you'll have to use the image you linked. The secret is to resize it by duplicating the "middle" column and row of pixels in the style of an Android 9-patch image, rather than by scaling the entire thing.

著墨染雨君画夕 2024-11-26 03:33:47

像这样的图像可以通过使其可拉伸来使用。基本上,您希望在调整图像大小时仅拉伸图像的中间部分,而不应缩放左右端盖,以避免失真。

UIImage *buttonImage = [UIImage imageNamed:@"UINavigationBarDefaultButton.png"];
UIImage *stretchableButtonImage = [buttonImage stretchableImageWithLeftCapWidth:5 topCapHeight:0];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:stretchableButtonImage forState:UIControlStateNormal];

但是,在您的情况下,为什么不只使用 UIBarButtonItem 并将 UIBarButtonItemStyleDoneUIBarButtonItemStyleBordered 作为其样式呢?

Images like that are used by making them stretchable. Basically, you want only the middle of the image to be stretched when you resize the image, while the left and right cap should not be scaled, to avoid distortion.

UIImage *buttonImage = [UIImage imageNamed:@"UINavigationBarDefaultButton.png"];
UIImage *stretchableButtonImage = [buttonImage stretchableImageWithLeftCapWidth:5 topCapHeight:0];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:stretchableButtonImage forState:UIControlStateNormal];

In your case however, why don't you just use a UIBarButtonItem with the UIBarButtonItemStyleDone or UIBarButtonItemStyleBordered as its style?

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