ios - 可调整大小的 UIImage,其中外部拉伸(stretch)和内部保持不变

标签 ios ios5 uiimageview uiimage

我正在尝试为我的应用程序中的垂直条创建一个“ handle ”,以向用户表明他们可以在 View 上滑动。我想只使用一个小版本的图像,它有抓地力和 1 px 上下它是酒吧其余部分的背景。所以:

ssss          ssss
gggg  becomes ssss
ssss          gggg
              ssss
              ssss

resizableImageWithCapInsets: 方法允许您告诉 iOS 不要调整外边缘,但有没有办法告诉它不要调整内部并随外部拉伸(stretch)?

我已经尝试指定一个超大的帽插图,UIEdgeInsetsMake(29.0f, 0.0f, 29.0f, 0.0f) 并且这导致 handle 位于图像框架的顶部,但是otherwise 是我正在寻找的正确拉伸(stretch)行为。我只需要将抓握部分放在框架的中央。

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

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];

更新基于所选答案的工作代码

@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

最佳答案

执行此操作的最简单方法可能是让您的抓握部分成为一个较大的条形 View 的(小高度,但全宽) subview ,其背景拉伸(stretch)或平铺。您可以通过自动调整蒙版等方式使抓握部分在较大 View 中居中。

关于ios - 可调整大小的 UIImage,其中外部拉伸(stretch)和内部保持不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11217935/

相关文章:

ios - 如何在 iOS Swift 中实现 Firebase Microsoft OAuth?

ios - 如何使用 ios pdfkit 显示现有的 pdf 注释

ios - 单击单元格时 UIView 位于 UITableView 之上?

iphone - 我可以在 iTunes 中为我的应用指定不支持 IOS 5 吗?

iphone - 具有不同页面宽度的 UIScrollview

ios - iOS 中,当用户通过 UISwipeGestureRecognizer 在 ImageView 中向左滑动时,如何在 ImageView 中显示 NextImage?

ios - 旋转 UIImage 或 UIImageView 自定义度数

ios - 拖动 uiview iOS

iOS保持线程在后台运行

ios - 获取 UIImageView 的位置 - iOS