如何在 UISegmentedControl 中为图像制作图像插图?
我有带有图像的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定是否有一种简单的编程方法可以做到这一点。我所做的只是在 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.