Search code examples
iosuiviewuitextfieldcalayer

Reproducing the border of a UITextField


I want to fade in the border for a UITextField, so am recreating it myself in a UIView, using its layer:

    borderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, textfield.frame.size.width, textfield.frame.size.height)];
    [borderView.layer setBorderWidth:1.0];
    [borderView.layer setBorderColor:[UIColor grayColor].CGColor];
    [borderView.layer setMasksToBounds:YES];
    [borderView.layer setCornerRadius:10.0];
    [borderView.layer setShadowColor:[UIColor blackColor].CGColor];
    [borderView.layer setShadowOffset:CGSizeMake(0, 3)];
    [borderView.layer setShadowOpacity:0.8];
    borderView.alpha = 0.0;

I then add it to the UITextField directly:

    [textField addSubview: borderView];
    [textField sendSubviewToBack: borderView];

However, despite the sendSubviewToBack: call, the text field refuses to become first responder when it is touched.

This worked fine when I was adding an ImageView as a sibling view instead of a UIView.

How can I make the textfield receive input?


Solution

  • borderView.userInteraction = NO;
    

    Checked it in my sim 4.3.