4

OK folks,

I've searched all over and cannot seem to find a solution to my issue.

I am trying to use a sizeable image for a button background to get a custom look for a UIButton and to reduce the overall size of the app and because it seems like the right thing to do.

However, when I try to set the image on the button the button gets all weird looking and the corners do not match the standard corner radius of a regular UIButton.

I have tried creating several different sizes of images but nothing I do seems to work.

I know the caps are supposed to be even and I have that plus the 1 pixel middle to stretch.

My UIButton is 44 high. If I create an image that is 44 pixels high and 21 pixels wide and has the same rounded corner radius as the default button (it likes an aspirin caplet) and set my background image like this:

UIImage *btnImage = [UIImage imageNamed:@"buttontest1.png"];
UIImage *newImg = [btnImage stretchableImageWithLeftCapWidth:10 topCapHeight:0];

the corners just don't match and look weirdly widely stretched AND the button grows in height!

I know the stretchableImagewithLeftCapWidth is deprecated but the usage of resizableCapWithInsets makes even less sense to me and when I try to do that the button image just seems to repeat over and over.

Can anyone figure out what I'm doing wrong? Is there anyplace that explains this crap simply? I just cannot seem to get it.

Thanks! -TJ

EDIT - adding images after using the resizableImageCapWithInserts to show results. They can't be typical as I see examples all over that supposedly work but the examples never work for me. I just figured out how to add images here so maybe this helps some.

My PNG file 21pxels wide by 29 pixels tall Here is the PNG file 21px wide by 39px tall

Results of my code This is the result of using the following code to set the background image:

[self.button1 setBackgroundImage:[[UIImage imageNamed:@"buttontest4.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)] forState:UIControlStateNormal];

As I understand it this should copy the left 10 pixels and right 10 pixels of my 21 pixel wide image as is and stretch the middle 1 pixel across, which it appears to do but it is also making my button get larger vertically and I'm getting this weird repeat. It should be the same size as the BTN next to it.

Here is my Xcode layout: Xcode layout

No matter what image I use I see similar results.

I'm obviously not groking something here.

I appreciate the answers so far. It's becoming slightly less foggy.

TJ

EDIT2: showing samples using the cap insets method with image 21px by 44px.

All 4 buttons are 44px high when designed in storyboard.

both buttons 44px high As you can see the orange buttons are both larger than the white buttons for scale comparison.

The top button is button1, bottom is button2.

I found a way to get it closer by using the optional resizingMode parameter of UIImageResizingModeStretch.

However, notice the buttons are larger than what they should be.

Here is the code for setting the button images.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self.button1 setBackgroundImage:[[UIImage imageNamed:@"buttontest1a.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)] forState:UIControlStateNormal];

[self.button2 setBackgroundImage:[[UIImage imageNamed:@"buttontest1a.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10) resizingMode:UIImageResizingModeStretch]  forState:UIControlStateNormal];
}

Doing the top image with (10,10,10,10) gets me an image that does not repeat the top part of the image like previously. I know the top image is not using the optional resize parameter as this is a test to see what each gets me.

Now, to complicate things even more. If I set my top button size to 43, that is one pixel smaller than my image and make a call with the optional resize parameter I get nearly perfect results except my button is not the right size.

What the heck is going on here?

I appreciate everyone who is trying to pound some knowledge through my thick skull. TJ

buttons 43 and 44

4

3 回答 3

7

UIEdgeInsets 有点难以理解,但它更现代的用法。我将解释它的工作方式。基本上,使用四个偏移量,您将图像分成 9 个切片。如果您想查看这可能的样子,请查看此页面的“可缩放区域”部分(注意,它适用于 Android,但概念是相同的。Android 只是首先这样做)。您会注意到四行穿过图像。这些将对应于您的四个插图。所以你的九个部分,从左到右和从上到下将是:

  1. X: 0 -> 左插图,Y: 0 -> 顶部插图
  2. X:左插入 ->(宽度 - 右插入),Y:0 -> 顶部插入
  3. X:(宽度 - 右插图)-> 宽度,Y:0 -> 顶部插图
  4. X:0 -> 左插入,Y:顶部插入 ->(高度 - 底部插入)
  5. X:左插入 ->(宽度 - 右插入),Y:顶部插入 ->(高度 - 底部插入)
  6. X:(宽度 - 右插图)-> 宽度,Y:顶部插图 ->(高度 - 底部插图)
  7. X:0 -> 左插图,Y:(高度 - 底部插图)-> 高度
  8. 左插入 ->(宽度 - 右插入),Y:(高度 - 底部插入)-> 高度
  9. X:(宽度 - 右插图)-> 宽度,Y:(高度 - 底部插图)-> 高度

第 1、3、7 和 9 部分已设置,不会拉伸。
第 2 节和第 8 节将仅水平拉伸
第 4 节和第 6 节将仅垂直拉伸
第 5 节将在两个方向上拉伸。

iOS 6 中的新功能,您可以选择一种模式。您可以拉伸可拉伸的瓷砖,或者重复它们以获得您想要的效果(颜色等应该拉伸,而纹理应该重复)。

于 2012-10-01T01:19:44.877 回答
5

试试 UIEdgeInsets

[button setBackgroundImage:[[UIImage imageNamed:@"buttontest1.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)] forState:UIControlStateNormal];
于 2012-10-01T00:41:24.230 回答
1

2016 年的方式是围绕这一点执行 WWDC2016 视频 213 中告诉我们的操作:https ://developer.apple.com/videos/play/wwdc2016-213/?time=1084 我们可以拉伸资产。对不起,这次只是一个链接;我稍后会提供更多细节。视频中的说明最多一两分钟。简而言之,我们使用 Xcode 的 Asset Slicer 工具来定义当图像用作 UIButton 的背景时,图像的哪些部分不应被拉伸。UIButton 可以扩展到任何大小而不会扭曲圆边等。

于 2016-11-25T01:51:11.247 回答