我試圖在自定義UIView
上放置自定義UIButton
,但未顯示UIButton
。儘管當我嘗試打印我的UIView
的UISubViews
時,它在那裏顯示了UIButton
對象。顯示自定義UIButton的問題
下面是一塊我寫把按鈕上我的自定義UIView
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.headerLabel.backgroundColor = [UIColor clearColor];
self.headerLabel.textColor = [UIColor whiteColor];
self.headerLabel.font = [UIFont systemFontOfSize:kApplicationHeaderTextFont];
[self addSubview:self.headerLabel];
self.actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.actionButton.frame = CGRectZero;
[self addSubview:self.actionButton];
}
return self;
}
- (void)drawRect:(CGRect)iTotalRect{
if (self.actionButtonImage) {
self.actionButton.frame = CGRectMake(self.frame.size.width - self.actionButtonImage.size.width - 10.0, self.frame.size.height/2 - self.actionButtonImage.size.height/2, self.actionButtonImage.size.width, self.actionButtonImage.size.height);
[self.actionButton setImage:self.actionButtonImage forState:UIControlEventTouchUpInside];
}
任何人知道我在做什麼錯誤的代碼?
這是一個非常糟糕的主意,做任何內部drawRect,你不必絕對必須如繪圖。它要慢得多,並且經常會有奇怪的行爲。也就是說,如果你註釋掉你的drawRect函數並在init函數中設置你的按鈕,它會顯示出來嗎? – SethHB