ios 为 UIButton 拉伸背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12623961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:26:44  来源:igfitidea点击:

Stretch background image for UIButton

iosuibuttonuiimagetextures

提问by Eugene Trapeznikov

I got texture, which really shorter, than my UIButton.

我得到了比 UIButton 更短的纹理。

I got this texture:

我得到了这个纹理:

enter image description here

在此处输入图片说明

And i should create this button:

我应该创建这个按钮:

enter image description here

在此处输入图片说明

How should i stretch (not tile), this texture? Stretching in horizontal direction

我应该如何拉伸(不是平铺),这种纹理?水平方向拉伸

Thnx

谢谢

回答by DrummerB

From the example images you provided I'm pretty sure you're looking for UIImage's resizableImageWithCapInsets:

从您提供的示例图像中,我很确定您正在寻找UIImage'sresizableImageWithCapInsets:

UIImage *originalImage = [UIImage imageNamed:@"myImage.png"];
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
UIImage *stretchableImage = [originalImage resizableImageWithCapInsets:insets];
[myButton setBackgroundImage:stretchableImage forState:UIControlStateNormal];
// the image will be stretched to fill the button, if you resize it.

The values in the UIEdgeInsets struct determine the margins you don't want to be stretched. Your left and right values could be about this wide:

UIEdgeInsets 结构中的值决定了您不想拉伸的边距。您的左右值可能大约是这样:

enter image description here

在此处输入图片说明

The top and bottom values can be either 0 (if you don't want to resize the button vertically), or probably half of the total height.

顶部和底部的值可以是 0(如果您不想垂直调整按钮的大小),也可以是总高度的一半。

回答by Anton Gaenko

With Xcode 6 (and iOS7+target) you can use slicing editor when working with images assets. Toggle slicing mode with Editor -> Show Slicingmenu or press Show Slicingbutton when select specific image with editor (showed below).

使用 Xcode 6(和iOS7+目标),您可以在处理图像资产时使用切片编辑器。使用编辑器切换切片模式-> 显示切片菜单或在使用编辑器选择特定图像时按显示切片按钮(如下所示)。

Show Slicing button

显示切片按钮

Then you can select image for specific display scale and drag rules or edit insets values manually.

然后您可以选择特定显示比例的图像和拖动规则或手动编辑插图值。

Rules

规则

After that you can select this image in Interface Builderfor UIButtonBackground Image(IB button's representation could look bad, but it should be OK when running).

之后,您可以在界面生成器中UIButton背景图像选择此图像(IB 按钮的表示可能看起来很糟糕,但运行时应该没问题)。

My buttons look well (running iOS 7.1 simulator and iOS 8 device).

我的按钮看起来不错(运行 iOS 7.1 模拟器和 iOS 8 设备)。

enter image description here

在此处输入图片说明

This Apple doc linkcould be helpful.

这个 Apple 文档链接可能会有所帮助。

回答by David H

By not tiling, I assume you can thus not use

通过不平铺,我假设您因此不能使用

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

The way to do what you want is to use:

做你想做的事情的方法是使用:

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0); // should be proportionally larger
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, (CGRect){{0,0}, newSize}, [yourImage CGImage]);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;

回答by jki

Use stretchableImageWithLeftCapWidth:topCapHeight:method.

使用stretchableImageWithLeftCapWidth:topCapHeight:方法。

Refer: https://github.com/boctor/idev-recipes/tree/master/StretchableImages

参考:https: //github.com/boctor/idev-recipes/tree/master/StretchableImages

回答by htafoya

Or using Stretchable Images:

或使用可拉伸图像

UIImage* imgbkg =[[UIImage imageNamed: @"my_bkg_image.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];

[_button setBackgroundImage: imgbkg forState: UIControlStateNormal];

Use 0if you don't want to stretch certain side.

如果您不想拉伸某侧,请使用0