Note that there are some explanatory texts on larger screens.

plurals
  1. POUITextField embedded in an UIView does not respond to touch events
    primarykey
    data
    text
    <p>In my project i'm using a UITextField which is embedded into a plain white UIButton with rounded corners in order to create a "beautiful" text field. This code is used several times in different views so I decided to create a custom UIView for this purpose. The code for this custom UIView:</p> <p>BSCustomTextField.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface BSCustomTextField : UIView { UIButton* btnTextFieldContainer; UITextField* textField; NSString* text; } @property (retain, nonatomic) UIButton* btnTextFieldContainer; @property (retain, nonatomic) UITextField* textField; @property (retain, nonatomic) NSString* text; -(id)initWithPosition:(CGPoint)position; @end </code></pre> <p>BSCustomTextField.m</p> <pre><code>#import "BSCustomTextField.h" #import &lt;QuartzCore/QuartzCore.h&gt; @implementation BSCustomTextField @synthesize btnTextFieldContainer, textField, text; -(id)initWithPosition:(CGPoint)position{ if (self == [super init]) { btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)]; btnTextFieldContainer.backgroundColor = [UIColor whiteColor]; [[btnTextFieldContainer layer] setCornerRadius:3.]; [[btnTextFieldContainer layer] setMasksToBounds:YES]; [[btnTextFieldContainer layer] setBorderWidth:0.]; textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)]; textField.backgroundColor = [UIColor whiteColor]; textField.clearButtonMode = UITextFieldViewModeAlways; textField.returnKeyType = UIReturnKeySend; textField.keyboardType = UIKeyboardTypeEmailAddress; textField.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.]; [btnTextFieldContainer addSubview:textField]; [self addSubview:btnTextFieldContainer]; } return self; } -(void)dealloc{ [btnTextFieldContainer release]; [textField release]; [super dealloc]; } @end </code></pre> <p>So, when using this view in the <em>viewDidLoad</em> of the container view (see code below) the view is properly rendered on the desired position and looks exactly as specified but it does not react on touch events and thus does not become first responder when touched.</p> <p>Code:</p> <pre><code>searchTextField = [[BSCustomTextField alloc] initWithPosition:CGPointMake(30, 150)]; searchTextField.textField.placeholder = NSLocalizedString(@"lsUserName", @""); [[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:searchTextField]; </code></pre> <p>Calling <em>[searchTextField.textField becomeFirstResponder]</em> programmatically works properly and makes the keyboard to appear.</p> <p>But, the interesting part is, if I embed the code of <em>BSCustomTextField</em> inline in my container just like this:</p> <pre><code>UIButton* btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(30, 150, 260, 50)]; btnTextFieldContainer.backgroundColor = [UIColor whiteColor]; [[btnTextFieldContainer layer] setCornerRadius:3.]; [[btnTextFieldContainer layer] setMasksToBounds:YES]; [[btnTextFieldContainer layer] setBorderWidth:0.]; searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)]; searchTextField.backgroundColor = [UIColor whiteColor]; searchTextField.clearButtonMode = UITextFieldViewModeAlways; searchTextField.returnKeyType = UIReturnKeySend; searchTextField.keyboardType = UIKeyboardTypeEmailAddress; searchTextField.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.]; searchTextField.placeholder = NSLocalizedString(@"lsUserName", @""); [btnTextFieldContainer addSubview:searchTextField]; [[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:btnTextFieldContainer]; [btnTextFieldContainer release]; </code></pre> <p>everything works as expected and the textfield reacts on touches. The type of the searchTextField is properly set in the header file for each case. The only difference is that in the first case I have an additional wrapping UIView on the UIButton and the UITextFiled. I have no idea what to do to make the text field to become first responder on touch.</p> <p>Thanks a lot in advance, Roman</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload