如何在 UISegmentedControl 中为图像制作图像插图?

发布于 2024-10-14 22:21:49 字数 806 浏览 3 评论 0原文

我有带有图像的 UISegmented 控件 - 能够设置背景色调。这是通过 viewDidLoad 中的代码添加到导航栏的:

UISegmentedControl *myCustomButton = [[UISegmentedControl alloc] initWithItems:
                    [NSArray arrayWithObjects:[UIImage imageNamed:@"spiral"],nil]];

[myCustomButton setSegmentedControlStyle:UISegmentedControlStyleBar];
[myCustomButton setTintColor:[UIColor blackColor]];

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc]
                                    initWithCustomView:myCustomButton];
self.navigationItem.rightBarButtonItem = segmentBarItem;

问题是按钮内的图像被拉伸,所以我想使用图像插图,但我该如何调用他们的代码呢?

我已经尝试过这个:

[self.navigationItem.rightBarButtonItem setImageInsets:UIEdgeInsetsMake(5, 5, 5, 5)];

但它没有做任何可见的事情。

I have UISegmented control with image -to be able to set background tint. This is added to navigation bar through code in viewDidLoad:

UISegmentedControl *myCustomButton = [[UISegmentedControl alloc] initWithItems:
                    [NSArray arrayWithObjects:[UIImage imageNamed:@"spiral"],nil]];

[myCustomButton setSegmentedControlStyle:UISegmentedControlStyleBar];
[myCustomButton setTintColor:[UIColor blackColor]];

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc]
                                    initWithCustomView:myCustomButton];
self.navigationItem.rightBarButtonItem = segmentBarItem;

problem is that the image within the button gets stretched all the way it can, so I would like to use image insets, but how do I call them for that code?

I've tried this:

[self.navigationItem.rightBarButtonItem setImageInsets:UIEdgeInsetsMake(5, 5, 5, 5)];

but it does not do anything visible.

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

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

发布评论

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

评论(1

甜尕妞 2024-10-21 22:21:49

我不确定是否有一种简单的编程方法可以做到这一点。我所做的只是在 Photoshop(或任何图像编辑器)中制作具有透明背景的 png 图像,并使它们适合,以便您的图像垂直适合按钮,并且大小正好适合您的按钮。制作图像需要一些工作,但通常一旦设置了屏幕布局,您就不会更改按钮大小。

I'm not sure there's an easy programmatic way to do this. What I've done is just make png images with transparent backgrounds in photoshop (or whatever image editor) and make them fit so that your image fits vertically in the button and has a size that will fit just about right in your button. It's a bit of work to make the images, but usually you're not going to be changing your button size once you have the screen layout set up.

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