Pregunta

No puedo interceptar la función init que se llama cuando se crea dentro del archivo xib.

Quiero agregarle un límite cuando se cree para no tener que agregarlo manualmente.

.h:

#import <UIKit/UIKit.h>

@interface UITextView (FITAddBorderline)
- (id) init;
- (id) initWithFrame:(CGRect)frame;
@end

.metro:

#import "UITextView+FITAddBorderline.h"

@implementation UITextView (FITAddBorderline)

- (id) init
{
    self = [super init];
    if (self) {
        [self addBorderline];
    }
    return self;
}

- (id) initWithFrame:(CGRect)frame {

    self = [super init];
    if (self) {
        self.frame = frame;
        [self addBorderline];
    }
    return self;
}

- (void) addBorderline {

    //To make the border look very close to a UITextField
    [self.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
    [self.layer setBorderWidth:2.0];

    //The rounded corner part, where you specify your view's corner radius:
    self.layer.cornerRadius = 5;
    self.clipsToBounds = YES;
}

@end
¿Fue útil?

Solución

vistas que provienen de las puntas se inicializan con initWithCoder:

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

    if (self)
    {
        [self addBorderline];
    }

    return self;
}

Como nota lateral, recomendaría cambiar lo que está haciendo y usar una subclase en lugar de una categoría.Puede ponerse en algunos métodos de problemas para anular en una categoría. Ver más información aquí.

Otros consejos

Sólo necesitas implementar el awakeFromNib método:

-(void)awakeFromNib

{
    [super awakeFromNib];
    [self addBorderline];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top