可调整大小的UIImage,其中外部延伸和内部保持相同 [英] Resizable UIImage where outside stretches and inside is kept the same

查看:65
本文介绍了可调整大小的UIImage,其中外部延伸和内部保持相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用中的竖条创建抓地力,以向用户显示他们可以在视图上滑动。我想使用一个小版本的图像,它有一个抓地力,上面和下面1个像素,这是酒吧其余部分的背景。所以:

I'm trying to create a "grip" for a vertical bar in my app, to show the user that they can swipe on the view. I'd like to just use a small version of the image, which has the grip and 1 px above and below it which is the background for the rest of the bar. So:

ssss          ssss
gggg  becomes ssss
ssss          gggg
              ssss
              ssss

方法 resizableImageWithCapInsets:允许你告诉iOS不要调整外边缘,但有没有办法告诉它不调整内部和外部伸展?

The method resizableImageWithCapInsets: allows you to tell iOS not adjust the outside edge, but is there a way to tell it to not adjust the interior and stretch with the exterior?

我试过指定一个超大的帽子插入, UIEdgeInsetsMake(29.0f,0.0f,29.0f,0.0f),这导致抓握位于图像框架的顶部,但是否则是正确的拉伸我正在寻找的行为。我只需要将握把部分放在框架中心。

I've tried specifying a oversized cap inset, UIEdgeInsetsMake(29.0f, 0.0f, 29.0f, 0.0f) and that resulted in the grip being at the top of the image's frame, but otherwise is the correct stretching behavior I'm looking for. I just need to have the grip part centered in the frame.

感兴趣的人的完整代码(这会导致整个长度重复抓握):

Full code for those interested (this results in the grip being repeated across the whole length):

gripBar = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"grip"] resizableImageWithCapInsets:UIEdgeInsetsMake(1.0f, 0.0f, 1.0f, 0.0f)]];
gripBar.frame = CGRectMake(0, 0, gripWidth, fullHeight);
[view gripBar];

更新:根据所选答案的工作代码

Update: working code based on chosen answer

@implementation GripBar

- (id)init {
    self = [super init];
    if (self) {
        UIImageView *bar = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"gripback"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, 0.0f, 1.0f, 0.0f)]];
        bar.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        [self addSubview:bar];

        UIImageView *grip = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"grip"]];
        grip.contentMode = UIViewContentModeCenter;
        grip.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        [self addSubview:grip];
    }
    return self;
}

@end


推荐答案

最简单的方法可能是让您的握把部分成为背景拉伸或平铺的较大条形视图的(小高度,但全宽)子视图。例如,您可以通过自动调整遮罩将握把部分保持在较大视图的中心位置。

The easiest way to do this is probably to make your grip part be a (small-height, but full-width) subview of a larger bar view whose background stretches or tiles. You can keep the grip part centered in your larger view by, for example, autoresize masks.

这篇关于可调整大小的UIImage,其中外部延伸和内部保持相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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