Question

I have this subclass of UITextField and I would like to set a property as background color, border etc of UITextField. I don't know, if I use right method, because when I use this class for a UItextField, the UITextField doesn't change.. In which method I have to declare this properties?

#import "defaultUITextField.h"

@implementation defaultUITextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        [self setBackgroundColor:([UIColor redColor])];

        UIColor *borderColor = [UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:233.0/255.0];

        self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
        self.leftViewMode = UITextFieldViewModeAlways;

        self.layer.borderColor = [borderColor CGColor];
        self.layer.borderWidth = 1.0f;
    }
    return self;
}

@end
Was it helpful?

Solution

your solution seems to be right. how did you place your suctom TextField on a View? If you created your TextField in Interface Builder then you overrided the wrong constructor.

- (id)initWithCoder:(NSCoder *)inCoder {
    if (self = [super initWithCoder:inCoder]) {
        self.delegate = self;
        [self setBackgroundColor:([UIColor redColor])];

        UIColor *borderColor = [UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:233.0/255.0];

        self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
        self.leftViewMode = UITextFieldViewModeAlways;

        self.layer.borderColor = [borderColor CGColor];
        self.layer.borderWidth = 1.0f;
    }
    return self;
}

this constructor is called when you create UITextField in IB and change its class to UItextField.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top