2011-10-04 15 views
8

He creado una Subclase de UIButton para darle un borde blanco 2 radios Sin Radios, y ahora estoy tratando de "globalmente" establecer su fuente, color de fuente y color de fondo dependiendo del estado del botón.UIButton Subclase - ¿Configurando las Propiedades?

La fuente no se establece. Tampoco los colores o los colores de fondo. ¿Que esta pasando? Aquí está mi código. Espero que puedas ayudar :)

- (id)initWithFrame:(CGRect)frame { 
self = [super initWithFrame:frame]; 
if (self) { 

    [[self titleLabel] setFont:[UIFont fontWithName:@"ZegoeUI" size:18]]; 
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; 
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    if(self.state == UIControlStateHighlighted) { 
     [self setBackgroundColor:[UIColor whiteColor]]; 
    } 
    else { 
     [self setBackgroundColor:[UIColor blackColor]]; 
    } 
} 
return self; 
} 





// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect { 

      [self.layer setMasksToBounds:YES]; 
[self.layer setBorderWidth:2.0]; 
[self.layer setCornerRadius:0.0]; 
[self.layer setBorderColor:[[UIColor colorWithWhite:1.0 alpha:1.0] CGColor]]; 
} 

No creo que esté haciendo nada mal. Tengo esta clase, y han vinculado Botones de IB, y establecer el tipo de clase ...

Gracias,

benben

Respuesta

17

Si ha creado los botones subclase personalizada en IB, initWithFrame: no habrá llamado. Debe sobrescribir initWithCoder:, o, preferiblemente, crear un método de instalación por separado llamado initWithFrame: y initWithCoder:.

Para el primer caso:

- (id)initWithCoder:(NSCoder*)coder { 
self = [super initWithCoder:coder]; 
if (self) { 

    [[self titleLabel] setFont:[UIFont fontWithName:@"ZegoeUI" size:18]]; 
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; 
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    if(self.state == UIControlStateHighlighted) { 
     [self setBackgroundColor:[UIColor whiteColor]]; 
    } 
    else { 
     [self setBackgroundColor:[UIColor blackColor]]; 
    } 
} 
return self; 
} 
+0

¿Cómo exactamente voy a hacer eso? Nunca he estado en la universidad, todo es autodidacta hasta ahora ... – topLayoutGuide

+0

Tú y yo ambos :). Vea la respuesta actualizada – jrturton

0

UIButton es un grupo clase. Realmente no deberías estar subclases ya que las clases que representa son privadas.

+1

No es técnicamente un clúster de clase, pero tiene un buen punto, puede complicarse al crear subclases (consulte http://stackoverflow.com/questions/816024/how-to-override-drawrect-in-uibutton- subclase), y la subclase UIControl puede ser una mejor apuesta. – jrturton

1

Así es como lo hago (sin IB):

+ (instancetype)customButtonWithCustomArgument:(id)customValue { 
    XYZCustomButtom *customButton = [super buttonWithType:UIButtonTypeSystem]; 
    customButton.customProperty = customValue; 
    [customButton customFunctionality]; 
    return customButton; 
} 

Works también con otros tipos, UIButtonTypeSystem es sólo un ejemplo.

Cuestiones relacionadas