我希望能夠使用scale9類型的背景圖像製作具有圖像背景的自定義按鈕,這意味着按鈕的寬度可以是動態的。我曾經在網頁上看到過每個按鈕都會這樣做的例子,但是在我看來,創建一個新的對象,這個對象的子類可以在界面設計器中用作任何自定義類按鈕(圓形矩形按鈕設置爲自定義)。iPhone/iPad有彈性,可調整大小的按鈕 - 繼承UIButton
這是我到目前爲止。
#import <Foundation/Foundation.h>
@interface LargeButton : UIButton {
}
@end
#import "LargeButton.h"
@implementation LargeButton
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.frame = CGRectMake(0, 0, 170.0, 48.0);
// Center the text vertically and horizontally
self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
UIImage *image = [UIImage imageNamed:@"btnBigPurple.png"];
// Make a stretchable image from the original image
UIImage *stretchImage =
[image stretchableImageWithLeftCapWidth:15.0 topCapHeight:0.0];
// Set the background to the stretchable image
[self setBackgroundImage:image forState:UIControlStateNormal];
// Make the background color clear
//self.backgroundColor = [UIColor clearColor];
}
return self;
}
@end
然而,這並不似乎工作。當我運行這個即時通訊模擬器時,我看到按鈕文本,但按鈕沒有背景。我已經放置了一個斷點,並且我知道它正在運行並檢查控制檯並且沒有錯誤。
有人可以幫忙嗎?解決這個問題還是我的思維方式錯了嗎?
謝謝
我記得在某處讀取UIButtons中的圖像必須具有某些RGB圖像標準。如果您沒有符合該圖像的圖像,則不會顯示。不記得在哪裏,所以我沒有太多的幫助:( – gabaum10 2010-10-21 19:46:10
感謝您的反饋。圖像工作,如果我把它設置在按鈕作爲背景沒有繼承UIButton並設置我的按鈕來使用我的對象,所以我不認爲它是一個圖像問題 – Chris 2010-10-22 10:19:08