How do you set stretchableImageWithLeftCapWidth in Interface builder?
How do 开发者_开发技巧you set stretchableImageWithLeftCapWidth in Interface builder?
Interface builder does not support stretchable images.
-(void) viewDidLoad {
[super viewDidLoad];
[myImageView setImage:[[myImageView image] stretchableImageWithLeftCapWidth:5 topCapHeight:5 ]];
}
This is currently (as of version 4.5) a limitation of InterfaceBuilder. However, having to create an IBOutlet
for each button and manually specify that the image is stretchable in viewDidLoad
is not a great solution, if for no other reasons than it makes creating the UI more cumbersome and also more brittle.
Instead of that, let's create a UIButton
subclass which will always make any background image stretchable by default. In this example I only stretch the normal and highlighted background image. For a production-ready class you would probably want to check all states of both background and foreground images and stretch them.
@implementation NTSTButton
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
/* There is no support in IB for stretchable images. So, let's
make sure _any_ background image assigned to this button
stretches correctly.
Note: Setting the stretch cap to 10px, which should work with
almost all button background images.
*/
UIImage *normalImage =
[[self backgroundImageForState:UIControlStateNormal]
stretchableImageWithLeftCapWidth:10 topCapHeight:10] ;
UIImage *highlightedImage =
[[self backgroundImageForState:UIControlStateHighlighted]
stretchableImageWithLeftCapWidth:10 topCapHeight:10] ;
[self setBackgroundImage:normalImage
forState:UIControlStateNormal];
[self setBackgroundImage:highlightedImage
forState:UIControlStateHighlighted];
}
return self;
}
@end
Similar to @answerbot, but updated for iOS 5.0+ as stretchableImageWithLeftCapWidth:topCapHeight:
is now deprecated.
@implementation UIButtonStretchable
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setBackgroundImage: [[self backgroundImageForState:UIControlStateNormal] resizableImageWithCapInsets:UIEdgeInsetsMake(22,6,22,6)]forState:UIControlStateNormal];
[self setBackgroundImage: [[self backgroundImageForState:backgroundImageForState:UIControlStateHighlighted] resizableImageWithCapInsets:UIEdgeInsetsMake(22,6,22,6)]forState:backgroundImageForState:UIControlStateHighlighted];
}
return self;
}
@end
iOS7 also introduced Asset catalogs where you can specify the stretchable area of your images, but that only works if you only support iOS7 (i.e, you don't care about users of the original iPad, iPod touch, or 3GS; yeah, there's no fragmentation in iOS).
See https://developer.apple.com/library/ios/recipes/xcode_help-image_catalog-1.0/SlicinganImage/SlicinganImage.html#//apple_ref/doc/uid/TP40013303-CH4-SW1
Use Stretching
configs in View
Section.
First of all, change View's mode to
Aspect fill
.Then edit X, Y, Width, Height field in
Stretching
part. For example: Let say you have an image size 10x20, and want to fit it in a view size 5x10 ==> Set X = 5/10 = 0.5, Y = 10/20 = 0.5
NOTE: IB doesnot allow you to use smaller image to "stretch" into bigger frame, so you need to design your image as large as possible
精彩评论