IOS 筆記 - 利用UIImage的stretchableImageWithLeftCapWidth方法給UIButton設定背景
阿新 • • 發佈:2019-01-29
學習ios開發,我使用的是這本書《iphone3開發基礎教程(完整版).pdf》這本書裡的demo不錯。
在我的下載空間裡有。可以免費下載。
今天看了一個範例,是利用UIImage的stretchableImageWithLeftCapWidth方法給UIButton設定背景,程式碼如下:
- (void)viewDidLoad { [super viewDidLoad]; UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"]; UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0]; [doSomethingButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"]; UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0]; [doSomethingButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted]; }
裡面設定了button在兩種狀態下的展示:UIControlStateNormal(非觸控狀態下)、UIControlStateHighlighted(觸控狀態下)
在該程式碼種用到了UIImage的 stretchableImageWithLeftCapWidth 以及 topCapHeight 方法,
這兩個函式是UIImage的一個例項函式,它的功能是建立一個內容可拉伸,而邊角不拉伸的圖片,需要兩個引數,第一個是左邊不拉伸區域的寬度,第二個引數是上面不拉伸的高度。
使用方法如上。
更多使用參考:http://www.cocoachina.com/bbs/read.php?tid-4156.html