當使用drawRect作爲UIButton子類時,我看起來好像從未被調用以在高亮時繪製按鈕。我需要在觸摸事件中爲我的按鈕調用setNeedsDisplay嗎?當爲UIButton子類使用drawRect時
2
A
回答
6
據我所知,沒有直接的方法來繼承UIButton。
UIButton不是初始化程序返回的實際類類型。 UIButton是一系列私人課程的前沿。
假設您有:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSLog(@"myButton type: %@", [myButton description]);
你會發現在日誌中返回的是「UIRoundedRectButton」類型。這個問題是你需要擴展「UIRoundedRectButton」。這是不可能的,因爲它是一個私人類,只有返回到UIButton。
最重要的是,「UIRoundedRectButton」不是唯一可能的返回類,它們都是私有的。
換句話說,UIButton的構建方式不適合擴展。
1
我有同樣的問題,並滿足成功與下面添加到我的UIButton
子
- (void)awakeFromNib {
[self addTarget:self action:@selector(redraw) forControlEvents:UIControlEventAllEvents];
}
- (void)redraw {
[self setNeedsDisplay];
[self performSelector:@selector(setNeedsDisplay) withObject:self afterDelay:0.15];
}
11
我知道這是一個老問題,但我遇到了同樣的問題,找到了一個簡單的解決方案:
只需將以下方法添加到您的UIButton子類中:
-(void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}
就是這樣!
相關問題
- 1. drawRect未調用PDFView子類
- 2. IBDesignable UIButton子類
- 3. UIView子類:drawRect不叫
- 4. 當調用setFrame時不調用drawRect
- 5. 使用特定的UIButtonType子類化UIButton
- 6. Ios UIButton使用OpenGL進行子類化
- 7. 在UIView子類中調用drawRect
- 8. 未在MKPinAnnotationView子類上調用drawRect
- 9. 使用自定義drawRect創建圓形UIButton:方法
- 10. UIButton子類:是否buttonWithType:返回UIButton或子類實例?
- 11. 更改UIButton矩形不帶drawRect
- 12. 當轉換爲swift 3.0時,在UIButton上模糊使用'dismiss()'
- 13. 我可以使用drawRect刷新UIView子類嗎?
- 14. 在IB中使用UITableViewCell的子類 - 爲什麼需要重寫drawRect:?
- 15. UIButton子類 - 設置屬性?
- 16. presentModalViewController在UIButton子類中
- 17. FindViewById在子類中使用時爲空
- 18. 使用drawRect重繪
- 19. 避免使用drawRect方法爲iPad
- 20. 使用UITabbaritem作爲UIButton
- 21. 當子類化UIView並將其添加到接口生成器中時,不會調用drawRect
- 22. XIV中的UIView子類不顯示(drawRect:從未調用過)
- 23. NSView的子類drawRect在無限循環中調用
- 24. drawRect沒有在我的UIImageView的子類中調用
- 25. iOS/Xcode - drawRect:不在UIView子類中調用
- 26. 儘管完全爲空drawRect,UIView子類仍然繪製背景: - 爲什麼?
- 27. 當我點擊目標c中的按鈕時調用drawRect?
- 28. NSView drawRect干擾子視圖?
- 29. 子視圖頂部的drawRect
- 30. 爲什麼的UIButton動作沒有被調用時的UIButton是
答案在這裏: http://stackoverflow.com/questions/4022763/change-background-color-of-uibutton-when-highlighted – GoldenBoy 2011-12-02 21:45:25