Bild:
[button1 setBackgroundImage:[UIImage imageNamed:@"redButton.png"]];
Ohne Bild:
[button1 setBackgroundColor:[UIColor redColor] ];
Mit diesem können Sie abgerundete Ränder bekommen:
CALayer * layer = [button1 layer];
[layer setCornerRadius:8.0f];
[layer setMasksToBounds:YES];
[layer setBorderWidth:1.0f];
[layer setBorderColor:[[UIColor whiteColor] CGColor]];
button1.backgroundColor = [UIColor redColor];
Für den Staat Unterklasse ausgewählt UIButton auf diese Weise:
In .h-Datei:
@interface MyButton : UIButton
{
@private
NSMutableDictionary *backgroundStates;
@public
}
- (void) setBackgroundColor:(UIColor *) _backgroundColor forState:(UIControlState) _state;
- (UIColor*) backgroundColorForState:(UIControlState) _state;
@end
in .m-Datei:
#import "MyButton.h"
@implementation MyButton
- (void) setBackgroundColor:(UIColor *) _backgroundColor forState:(UIControlState) _state {
if (backgroundStates == nil)
backgroundStates = [[NSMutableDictionary alloc] init];
[backgroundStates setObject:_backgroundColor forKey:[NSNumber numberWithInt:_state]];
if (self.backgroundColor == nil)
[self setBackgroundColor:_backgroundColor];
}
- (UIColor*) backgroundColorForState:(UIControlState) _state {
return [backgroundStates objectForKey:[NSNumber numberWithInt:_state]];
}
#pragma mark -
#pragma mark Touches
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UIColor *selectedColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateHighlighted]];
if (selectedColor) {
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.layer addAnimation:animation forKey:@"EaseOut"];
self.backgroundColor = selectedColor;
}
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
UIColor *normalColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateNormal]];
if (normalColor) {
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.layer addAnimation:animation forKey:@"EaseOut"];
self.backgroundColor = normalColor;
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
UIColor *normalColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateNormal]];
if (normalColor) {
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.layer addAnimation:animation forKey:@"EaseOut"];
self.backgroundColor = normalColor;
}
}
- (void) dealloc {
[backgroundStates release];
[super dealloc];
}
@end
Dann in Ihrem Viewcontroller (Denken Sie daran, Ihre benutzerdefinierte Klasse enthalten) können Sie die Farbe auf diese Weise einstellen:
[button1 setBackgroundColor:[UIColor colorWithRed:0.8 green:0.7 blue:0.6 alpha:1.0] forState:UIControlStateHighlighted];
siehe diese Frage: http: //stackoverflow.com/questions/948106/how-can-i-change-the-default-color-of-the-button-on-iphone – self
gibt es keine Möglichkeit, es ohne zu tun Bild? – cnaaniomer
Es gibt eine Antwort in diesem Post. funktioniert gut http://stackoverflow.com/questions/9737503/changing-uibutton-background-color – tiagouy