简体   繁体   English

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

[英]How to make image insets for image within UISegmentedControl?

I have UISegmented control with image -to be able to set background tint. 我有图像的UISegmented控件 - 能够设置背景色调。 This is added to navigation bar through code in viewDidLoad: 这将通过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. 但它没有任何可见的作用。

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. 我所做的只是在photoshop(或任何图像编辑器)中制作具有透明背景的png图像,并使它们合适,以使您的图像垂直适合按钮,并且大小恰好适合您的按钮。 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. 制作图像需要做一些工作,但是一旦设置了屏幕布局,通常就不会改变按钮的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM