简体   繁体   中英

How do i make my GradientButton redraw in my iPhone app?

I have an app with a button in the bottom that i'm creating using a UIButton library, GradientButton (found here: http://code.google.com/p/iphonegradientbuttons/ ).

I can create buttons fine, but i cannot for the world figure out how to change the colour gradient of the created button after i've set the color once...

I tried, as an example, to change the gradient in my button's actionlistener with various tricks, for example (i'm setting it in viewdidload the first time):

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

Am i missing something here? If i change any other, built-in component, say a regular button's text and/or background, all works fine.

If there's something i'm not getting regarding how objects are repainted in objective c, i'm all ears...

EDIT: after suggestion, i tried to add some code to alter the color arrays of the button. Unfortunately that didn't work either:

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

I found a solution. Add these lines to the begin of every use...Style method:

normalGradient = NULL;
highlightGradient = NULL;

and add [self setNeedsDisplay]; at the end of these methods.

After these changes it works!

depends on when/why you want to change it.

It looks like you can just use normalGradientColors and highlightedGradientColors to outright change the colors.

That...honestly seems like all you have to do. just change those properties...hand in an array of colors.

//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];

EDIT: added cast to CGColor. If you are using ARC you will have to add the bridge tag instead of the 'simple' cast

(__bridge id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM