2010-08-06 14 views
8

Tengo una subclase UIView personalizada. En IB, he especificado un UIView de "marcador de posición" y configuré la clase a mi nombre de clase. Mi anulación del método drawRect está funcionando, y el fondo se está coloreando correctamente, pero el initWithFrame no se está disparando. ¿Por qué?UIView Subclase initWithFrame no se llama?

- (id)initWithFrame:(CGRect)frame { 
    NSLog(@"initWithFrame"); 
    if ((self = [super initWithFrame:frame])) { 
     noteTextFont = [[UIFont boldSystemFontOfSize:12] retain]; 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 
    [super drawRect:rect]; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    UIColor *backgroundColor = [UIColor blackColor]; 
    [backgroundColor set]; 
    CGContextFillRect(context, rect); 

    UIColor *labelColor = [UIColor lightGrayColor]; 
    [labelColor set]; 
    [notificationLabel drawInRect:CGRectMake(44, 18, rect.size.width-20, rect.size.height) withFont:noteTextFont lineBreakMode:UILineBreakModeTailTruncation]; 

    [self setNeedsLayout]; 
} 
+0

¿Puedes mostrar el código donde creas la vista personalizada? ¿Está llamando a: \t UIView * fred = [[UIView alloc] initWithFrame: myFrame]; – joelm

Respuesta

-2

porque va a inicializar esta vista desde una punta que parece, por lo que se utiliza el valor por defecto initWithNibName (de UIView): en lugar de inicializar utilizando initWithFrame:

+0

Entonces, ¿es imposible anular la inicialización en la subclase? Otra pregunta. En IB, la vista se establece en 50 pt de alto, pero se está inicializando con un marco de 42pt de alto. –

+0

probaría anular init: también y ver si se llama. Olvidé que UIView en realidad no tiene initWithNibName. ese es un método en UIViewController –

52

cosas cargadas desde una punta están inited con -(id)initWithCoder:(NSCoder*)coder

+13

Deseo que la gente vuelva y marque las respuestas como correctas. –