2011-09-12 18 views
1

我有一個應用程序的底部,我創建使用UIButton庫,GradientButton(在這裏找到:http://code.google.com/p/iphonegradientbuttons/)的按鈕。如何讓我的GradientButton在我的iPhone應用程序中重繪?

我可以創建按鈕罰款,但我可以不爲世人弄清楚如何改變創建按鈕的顏色漸變我設置的顏色一次之後...

我想,作爲一個例子,

- (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. 
} 

我失去了一些東西在這裏:改變我的按鈕的各種技巧的ActionListener梯度,例如(我是第一次設置它在viewDidLoad中)?如果我改變任何其他內置組件,比如說一個普通按鈕的文本和/或背景,所有的都可以正常工作。

如果有什麼東西我沒有收到關於對象在目標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 

回答

0

我找到了解決方案。這些行添加到開始每use...Style方法:

normalGradient = NULL; 
highlightGradient = NULL; 

,並在這些方法的末尾添加[self setNeedsDisplay];

經過這些更改,它的工作原理!

+0

Whee,好東西。謝謝! – Mathias

0

取決於何時/爲什麼要改變它。

它看起來像你可以只使用normalGradientColorshighlightedGradientColors徹底改變顏色。

那......老實說好像你所要做的一切。只需改變這些屬性......交上一組顏色。

//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) 
+0

嘿,謝謝你的幫助。對我而言,這並不幸運。我編輯了我的問題,以表明我做了什麼。 *哭泣* :) – Mathias

+0

對不起,我有點倉促。我想過鑄造,但沒有提及它。 – griotspeak

+0

嘿夥計,maaan它仍然不適合我。什麼是intersting是在我的「viewdidload」我有[checkInCheckOutButton useAlertStyle];所以按鈕有這種顏色,但我沒有設置突出顯示。所以*第一次*我更改數組,突出顯示獲取設置,以便按下按鈕時不同,但在此之後,當我切換數組時沒有任何反應。不知何故,只有在以前沒有設置過時纔會發生變化....我很困惑! :) – Mathias

相關問題