Jakub
Jakub

Reputation: 13860

UIImage with resizableImage option

I want to set image as a background on my button. So i create an image:

Button

then I want to implement it as stretchable:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 200, 400)];  
[button setTitle:@"Test Button" forState:UIControlStateNormal];

UIImage *buttonImage = [_model.background resizableImageWithCapInsets:UIEdgeInsetsMake(6, 8, 6, 7)]; //this is screen 1
//UIImage *buttonImage = _model.background; //this is screen 2   

[button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];        
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self addSubview:button];

I want to background stretch like in screen 2 but corners will sharp like screen 1. _model.background is UIImage contains original button background from the top of this post.

Screen 1 Screen 2

Am I using it right? I follow the documentation so UIEdgeInsetsMake(top,left,bottom,right) should be unstreachable corners, right?

Upvotes: 1

Views: 358

Answers (2)

webjprgm
webjprgm

Reputation: 4571

According to the docs, the part inside the insets (the part not locked via cap settings) is tiled. So you should have a small 1 pixel region to tile. E.g. make the insets be UIEdgeIndetsMake(20,8,19,7)

This produces:

enter image description here

Upvotes: 2

holex
holex

Reputation: 24041

honestly, this is not the best picture for this effect but, please, try this line:

[button setBackgroundImage:[[UIImage imageNamed:@"image.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20.f, 62.f, 20.f, 62.f)] forState:UIControlStateNormal];

Upvotes: 1

Related Questions