Previously introduced through the Stretchableimagewithleftcapwidth:topcapheight: method to achieve a scalable picture;
See this essay: Using the Stretchableimagewithleftcapwidth method to achieve a scalable picture
A new UIImage object instance method is provided in IOS5: Resizableimagewithcapinsets: You can convert a picture to a scalable picture that is offset by an offset value (the picture content within the offset value will not be stretched or compressed).
Stretchableimagewithleftcapwidth:topcapheight: Mode (this method has expired, recommended with the following resizableimagewithcapinsets: method);
Resizableimagewithcapinsets: The pattern can also achieve similar image gradient effect, more flexible control.
The effect is as follows:
ViewController.h
1 #import <UIKit/UIKit.h>23@interface viewcontroller: Uiviewcontroller4@end
Viewcontroller.m
1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4- (void) Layoutui;5 @end6 7 @implementationViewcontroller8 #defineKwidthofbutton 160.09 #defineKheightofbutton 49.0Ten One- (void) Viewdidload { A [Super Viewdidload]; - - [self layoutui]; the } - -- (void) didreceivememorywarning { - [Super didreceivememorywarning]; + //Dispose of any resources the can be recreated. - } + A- (void) Layoutui { at //Normal Mode -UIImage *img = [UIImage imagenamed:@"BlueButton"];//size:39*39 -UIButton *btnnormal = [[UIButton alloc] Initwithframe:cgrectmake (20.0,40.0, Kwidthofbutton, Kheightofbutton)]; -[Btnnormal Settitle:@"Btnnormal"Forstate:uicontrolstatenormal]; - [Btnnormal setbackgroundimage:img forstate:uicontrolstatenormal]; - [Self.view Addsubview:btnnormal]; in - //stretchableimagewithleftcapwidth:topcapheight: Mode (this method has expired, recommended with the following resizableimagewithcapinsets: method) toUIImage *imgstretchable = [img stretchableimagewithleftcapwidth:16.0Topcapheight:16.0]; +UIButton *btnstretchable = [[UIButton alloc] Initwithframe:cgrectmake (20.0,50.0+Kheightofbutton, Kwidthofbutton, Kheightofbutton)]; -[Btnstretchable Settitle:@"btnstretchable"Forstate:uicontrolstatenormal]; the [btnstretchable setbackgroundimage:imgstretchable forstate:uicontrolstatenormal]; * [Self.view addsubview:btnstretchable]; $ Panax Notoginseng //resizableimagewithcapinsets: mode; compared to stretchableimagewithleftcapwidth:topcapheight: mode, Resizableimagewithcapinsets: Mode can also achieve similar image gradient effect, more flexible control -UIImage *imgresizable = [img Resizableimagewithcapinsets:uiedgeinsetsmake (30.0,16.0,9.0,16.0)]; theUIButton *btnresizable = [[UIButton alloc] Initwithframe:cgrectmake (20.0,60.0+ (kheightofbutton*2), Kwidthofbutton, Kheightofbutton)]; +[Btnresizable Settitle:@"btnresizable"Forstate:uicontrolstatenormal]; A [btnresizable setbackgroundimage:imgresizable forstate:uicontrolstatenormal]; the [Self.view addsubview:btnresizable]; + } - $ @end
Using the Resizableimagewithcapinsets method to implement a scalable picture