3

The UITextFields in my app have placeholder text defined (in Interface Builder), and I cannot cause these fields to acquire focus (i.e. show the keyboard and allow editing) when I tap on the area occupied by the placeholder text. If I tap on the textfields in an area just outside the that of placeholder text (though still within the bounds of the textfiled itself), it acts as normal (i.e. the keyboard pops up and I can edit the content of the textfield). How can I fix this?

Thanks.

EDIT 1

Ok, I think I've got it. I'm also setting a blank view to the "leftView" property of these UITextFields. If I remove this, you can touch the UITextFields in the area of the placeholder text and it reacts as expected; I need this view for the leftView though. If you change the background color of this spacer view to red, you can see that it doesn't get in the way at all, so I don't know what's going wrong.

Why does this code cause this problem?

Thanks.

+(UIView*)getTextFieldLeftSpacerViewWithBackgroundColor:(UIColor*)backgroundColor andHeight:(CGFloat)height
{
    UIView *leftWrapper = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 8.0f, height)];
    leftWrapper.autoresizingMask = UIViewAutoresizingNone;
    [leftWrapper setOpaque:YES];
    if(backgroundColor){leftWrapper.backgroundColor = backgroundColor;}
    else{leftWrapper.backgroundColor = [UIColor clearColor];}
    return [leftWrapper autorelease];
}

+(void)setTextFieldLeftSpacerForTextFieled:(UITextField*)textField
{
    if(textField)
    {
        UIView *spacer = [MYViewController getTextFieldLeftSpacerViewWithBackgroundColor:nil andHeight:textField.bounds.size.height];
        textField.leftView = spacer;
        textField.leftViewMode = UITextFieldViewModeAlways;
    }
}
Jawwad
  • 1,326
  • 2
  • 9
  • 18
RyanM
  • 5,680
  • 9
  • 45
  • 55
  • How are you setting the placeholder text? – jlehr May 30 '12 at 15:14
  • So first of all, when I drag a UITextField onto the Interface Builder, it does not have default text within it. I am using XCode 4.3.2. Are you assigning text within the GUI Designer? Tell me more :-) – trumpetlicks May 30 '12 at 15:18
  • @trumpetlicks Placeholder Text can be set directly in the Attributes Inspector in IB. The OP says that he set it in IB. A placeholder is not the same thing as default text by the way. – jlehr May 30 '12 at 15:21
  • I have just tested putting text in for the "text" and separately in the "Placeholder" fields in the Interface builder, and they seem to work fine. What are the other settings of your UITextField. Have you implemented any actions, or stroke by stroke based routines? And I knew that, placeholder text is really only a non-editable field used primarily as a reminder of what the user should be entering, while the "text" attribute can indeed be edited by the app user (potentially if your settings allow). – trumpetlicks May 30 '12 at 15:22
  • As I stated, the placeholder text is set in IB (there's a specific place to set that property in IB). Some of the textfields have delegates defined (if so, they all return YES for shouldBeginEditing), but others do not have a delegate defined. These checkboxes are all checked in IB for each textfield: opaque, clears graphics context, clip subviews, autoresize subviews, enabled, user interaction enabled, and adjust to fit. The border style is solid, left alignment, and the background color is set to white. – RyanM May 30 '12 at 15:29
  • 1
    I have to admit, I am trying to replecate your problem, I even have 2 UITextFields, 1 delegated to a UIViewController, and one not, and the Keyboard still comes up for both. They both have Placeholder text, no "text" text, and all of the same parameters checked that you do. – trumpetlicks May 30 '12 at 16:51
  • There is nothing unusual here that would cause this. Can you post the code for the view controller that this is in? – lnafziger May 30 '12 at 16:54

2 Answers2

7

Just ran into the same problem and didn't want to subclass, just had to use :

leftWrapper.userInteractionEnabled = NO;
HiDeoo
  • 10,353
  • 8
  • 47
  • 47
1

I abandoned this approach. Instead of using an invisible view to offset the text, I opted to subclass UITextField and provide offset CGRects for the bounds of the text within theUITextField. The following SO post was very helpful:

Indent the text in a UITextField

Community
  • 1
  • 1
RyanM
  • 5,680
  • 9
  • 45
  • 55