Question

I have a custom look designed for my textfields on iphone. I decided to create a class

MYTextField : UIView <UITextFieldDelegate>

I have a method

- (id)initWithPlaceholder:(NSString*)placeholder position:(CGPoint) position

that creates a UITextField with the desired view and look.

The sourcecode is as follows:

- (id)initWithPlaceholder:(NSString*)placeholder position:(CGPoint) position {
    if (self = [super initWithFrame:CGRectMake(position.x, position.y, TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT)]) {
        UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)] autorelease];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.font = [UIFont systemFontOfSize:15];
        textField.placeholder = @"enter text";
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        textField.keyboardType = UIKeyboardTypeDefault;
        textField.returnKeyType = UIReturnKeyDone;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
        [textField setUserInteractionEnabled:YES];
        textField.delegate = self;

        [self addSubview:textField];
    }
    return self;
}

The problem is that, on invoking the method, the UITextField is visible but on tapping on the textfield, the keyboard doesnt show up.

Is there anything I am missing?

PS: I found a couple of similar problems on Stackoverflow, but none of the solutions seem to work for me. :(

Was it helpful?

Solution

The issue here could be that the textfields frame doesn't lie within the frame of the UIView. Why use TEXTFIELD_WIDTH for the self.frame but not for textfield.frame?

Also, why subclass UIView rather that UITextField?

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