UITextField embedded in UIView does not respond to touch events

In my project, I use UITextField, which is embedded in a plain white UIButton with rounded corners to create a “pretty” text box. This code is used several times in different views, so I decided to create a custom UIView for this. Code for this custom UIView:

BSCustomTextField.h

#import <Foundation/Foundation.h>

@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

BSCustomTextField.m

#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>

@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

Thus, when using this view in the container's viewDidLoad view (see the code below), the view is properly displayed in the desired position and looks exactly as indicated, but it does not respond to touch events and thus does not become the first responder when touched .

the code:

searchTextField = [[BSCustomTextField alloc] initWithPosition:CGPointMake(30, 150)];
searchTextField.textField.placeholder       = NSLocalizedString(@"lsUserName", @"");
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:searchTextField];

[searchTextField.textField FirstResponder] .

, BSCustomTextField inline :

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];

, , . searchTextField . , UIView UIBiew UIButton UITextFiled. , , .

, Roman

+3
3

, . raase UIResponder, , - , : UIView UIView, .
BSCustomTextField.m

#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>


@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;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    // UIView will be "transparent" for touch events if we return NO
    return YES;
}

-(void)dealloc{
  [btnTextFieldContainer release];
  [textField release];
  [super dealloc];
}
@end 

BSCustomTextField, YES pointInside: withEvent:

, .

+4

UIResponder

becomeFirstResponder: isFirstResponder:

text editing YES

+1

, . UIImageView UITextField.

UIImage *backgroundImage = [[UIImage imageNamed:@"fieldBackground.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];

Or you can try disabling userInteraction in UIButton.

0
source

Source: https://habr.com/ru/post/1782564/


All Articles