在Xcode故事板中使用可伸缩的图像 [英] Using stretchable images in Xcode storyboards

查看:148
本文介绍了在Xcode故事板中使用可伸缩的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用故事板来布置我的视图控制器,我想为我的按钮使用可伸缩的图像(这样我就不需要生成不同大小的多个图像)。

I'm using storyboards to layout my view controllers and I would like to use stretchable images for my buttons (so that I don't need to generate several images with different sizes).

这是否可以直接在故事板中进行而无需编写任何代码?我真的很喜欢将故事板用于所有图形内容的可能性,并保持代码清洁UI的东西,但似乎我无法在这里摆脱它。

Is this possible to do directly in storyboards without writing any code? I'm really liking the possibility to use storyboards for all graphic stuff and keep the code clean from UI stuff, but it seems like I can't get away with it here.

如果不可能,你会建议用什么呢?

If it is not possible, what would you suggest otherwise?

推荐答案

iOS 7 +更新

iOS 7+现在通过资产目录本地支持可伸缩图像。使用资产目录,您现在可以指定图像的切片方式以及图像的缩放方式(拉伸或平铺)。图像的这些资产目录属性将立即反映在故事板中。伟大的新改进。有关详细信息,请参阅 Apple关于资产目录的文档

在7之前部署到iOS版本:

这是一个鲜为人知的事实,但你绝对可以设置图像的上限仅使用Interface Builder / Storyboard和属性检查器中的拉伸属性。感谢 Victor 的原始答案。

查看在 UIImage 的属性检查器中拉伸属性,X和Y值是拉伸起始点相对于图像的整个宽度和高度的位置。值0.5表示图像中间的一个点。

Looking at the stretching properties in the attributes inspector of a UIImage, the X and Y values are the positions for the stretch starting point, relative to the entire width and height of the image. A value of 0.5 would mean a point in the middle of the image.

宽度和高度是可伸展区域相对于图像大小的大小。因此,将宽度设置为1 / imageWidth的值会将可伸展区域设置为1px宽。

The width and height are sizes for the stretchable area relative to the image size. So, setting the width to a value of 1 / imageWidth would set the stretchable area to be 1px wide.

大多数可伸展图像将从中间伸展,因此使用这些值对于X,Y,宽度和&高度通常有效:

Most stretchable images will stretch from the middle, so using these values for X,Y, Width, & Height will usually work:

X = 0.5
Y = 0.5
Width = 1/imageWidth
Height = 1/imageHeight

注意:除非你有一个非常小的图像,你正在伸展,这个意味着宽度和高度属性将非常小(例如0.008),而0.0可以代替使用。因此,实际上,0.5,0.5,0.0,0.0几乎总是适用于X,Y,Width&高度。

Note: Unless you have a very small image you are stretching, this means that width and height properties will be very small (e.g. 0.008) and 0.0 can be used instead. So, practically speaking, 0.5, 0.5, 0.0, 0.0 will almost always work for X,Y, Width & Height.

在少数情况下,0.0不适用于宽度和高度,这意味着您需要使用计算器在IB中设置这些值。但是,我认为这通常比必须以编程方式设置它更好,因为您将能够在IB(WYSIWYG)中看到生成的拉伸图像。

In the small number of cases that 0.0 does not work for Width and Height this does mean you need to use a calculator to set these values in IB. However, I think that is generally preferable than having to set it programmatically as you will be able to see the resulting stretched image in IB (WYSIWYG).

更新:部分人们已经指出,尽管使用上述建议在Storyboard中放大图像,但是按钮上的拉伸图像仍然会被破坏,即使是iOS7也是如此。不用担心,通过创建 UIButton 类别可以轻松解决这个问题,该类别负责为控件状态设置上限内容:

Update: Some people have pointed out that although stretching images works in Storyboard using the above suggestions, stretching images on buttons is still broken, even as of iOS7. Not to worry, this is easily addressed by creating a UIButton category that takes care of setting the cap insets for control states:

@implementation UIButton (Stretchable)

/* Automatically set cap insets for the background image. This assumes that
   the image is a standard slice size with a 1 px stretchable interior */
- (void)setBackgroundImageStretchableForState:(UIControlState)controlState
{
    UIImage *image = [self backgroundImageForState:controlState];
    if (image)
    {
       CGFloat capWidth =  floorf(image.size.width / 2);
       CGFloat capHeight =  floorf(image.size.height / 2);
       UIImage *capImage = [image resizableImageWithCapInsets:
                     UIEdgeInsetsMake(capHeight, capWidth, capHeight, capWidth)];

       [self setBackgroundImage:capImage forState:controlState];
    }
}

使用此类别,您可以设置您的可伸缩图像您的按钮通过Storyboard,然后通过在 -viewDidLoad 中调用 -setBackgroundImageStretchableForState:轻松确保它正确延伸。迭代您的视图层次结构,即使对于视图中的大量按钮,也可以轻松完成此操作:

Using this category, you can set your stretchable image for your button via Storyboard and then easily ensure that it stretches properly by calling -setBackgroundImageStretchableForState: in your -viewDidLoad. Iterating through your view hierarchy makes it trivial to do this even for a large number of buttons in your view:

NSPredicate *predicate = 
    [NSPredicate predicateWithFormat:@"self isKindOfClass:%@",[UIButton class]];
NSArray *buttons = [self.view.subviews filteredArrayUsingPredicate:predicate];
for (UIButton *button in buttons)
   [button setBackgroundImageStretchableForState:UIControlStateNormal];

虽然这不如拥有 UIButton 子类为你自动执行此操作(子类化 UIButton 不实用,因为它是一个类集群),它确实为您提供了几乎相同的功能viewDidLoad中的样板代码 - 您可以在Storyboard中设置所有按钮图像,并使它们正常拉伸。

While this isn't quite as good as having a UIButton subclass which does this automatically for you (subclassing UIButton isn't practical since it's a class cluster), it does give you nearly the same functionality with just a bit of boilerplate code in your viewDidLoad -- you can set all your button images in Storyboard and still get them to properly stretch.

这篇关于在Xcode故事板中使用可伸缩的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆