[英]How do i make my GradientButton redraw in my iPhone app?
我有一個應用程序,該應用程序的底部有一個按鈕,該按鈕是使用UIButton庫GradientButton(在此處找到: http : //code.google.com/p/iphonegradientbuttons/ )創建的。
我可以很好地創建按鈕,但是我無法為世界確定在設置顏色一次后如何更改所創建按鈕的顏色漸變...
例如,我嘗試使用各種技巧來更改按鈕的動作監聽器中的漸變(例如,我是在第一次將其設置在viewdidload中):
- (IBAction)buttonPressed:(id)sender{
NSLog(@"buttonPressed;changing style");
[checkInCheckOutButton useAlertStyle];//build-in method in gradientbutton that sets colour
[checkInCheckOutButton setNeedsDisplay];//added this when first didn't work.
}
我在這里想念什么嗎? 如果我更改任何其他內置組件,例如常規按鈕的文本和/或背景,則一切正常。
如果關於物鏡如何在目標c中重繪我沒有得到什么,我會非常高興...
編輯:建議后,我試圖添加一些代碼來更改按鈕的顏色數組。 不幸的是,這也不起作用:
- (IBAction)buttonPressed:(id)sender{
NSLog(@"buttonPressed;changing style");
self.checkInCheckOutButton.normalGradientColors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor blueColor], nil];
self.checkInCheckOutButton.highlightGradientColors = [NSArray arrayWithObjects:[UIColor greenColor], [UIColor whiteColor], nil];
[self.checkInCheckOutButton setNeedsDisplay];//tried with and without
我找到了解決方案。 將這些行添加到每次use...Style
方法的開頭:
normalGradient = NULL;
highlightGradient = NULL;
並添加[self setNeedsDisplay];
在這些方法的末尾。
這些更改后,它就可以工作了!
取決於何時/為什么要更改它。
它看起來像你可以只使用normalGradientColors
和highlightedGradientColors
以徹底改變顏色。
誠實地看起來,這就是您要做的所有事情。 只需更改這些屬性即可。
//This changes the colors for UIControlStateNormal
theButton.normalGradientColors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor blueColor] CGColor], nil];
//This changes the colors for UIControlStateHighlighted
theButton.HighlightedGradientColors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor blueColor] CGColor], nil];
編輯:將演員表添加到CGColor。 如果您使用的是ARC,則必須添加網橋標記,而不是“簡單”強制轉換
(__bridge id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.