UIView inside UIView with TextField and Button not working

I have a few textfield and button inside a UIView but the click event is not getting called I have checked the Height of the UIView its more than my button and textview. As you can see in the screenshot of UIView:

_chemicalUIview -UIView

These are the screenshot of TextField and button

_Ans6Text1new1 -First TextField


_Remove1Chemical -Last Button



And in between there are lots of labels and textfield. As you can see in this image
The Second UIView which is marked in red is the UIVIew where the Textfield and button is not working. This is the Layout of My UIView

There are two UIView. In first UIView all the TextField and button is working and in the second UIView which is inside First UIView there no Button or textfield is working.

Also I have set

userInteractionEnabled=NO 
also tried userInteractionEnabled=YES

for the UIView and

userInteractionEnabled=YES;

for the TextField and Button but still no luck. I have no clue what to do next.This is my code


- (void)addChemicalTapped:(UIButton *)sender {

  _ans6Constraint.constant = 750;
// [self.addChemical addTarget:self action:@selector(fixDelaysContentTouches:) forControlEvents:UIControlEventTouchUpInside];


  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:_Ans6Text1new1];

  _chemicalUIview.userInteractionEnabled=NO;
// _chemical1scroll.userInteractionEnabled=false;
  _Ans6Text4new1.userInteractionEnabled=YES;
  _Ans6Text2new1.userInteractionEnabled=YES;
  _Ans6Text1new1.userInteractionEnabled=YES;
  _Ans6Text3new1.userInteractionEnabled=YES;
  [_RemoveChemical1 setTag:1234];
  [_RemoveChemical1 addTarget:self action:@selector(RemoveChemical1Tapped:) forControlEvents:UIControlEventTouchUpInside];
  [self reloadTable];
}


There is no problem with click event because the id of same textfield and button is getting called in different UIView. So there is some thing wrong with my UIView code. This is my UIView code


UIView *paddingView3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
  [_ans6Text1 setClipsToBounds:NO];
  [_ans6Text1.layer setBorderWidth:1.0];
  [_ans6Text1.layer setBorderColor:[UIColor whiteColor].CGColor];
  [_ans6Text1.layer setCornerRadius:4.0];
  [_ans6Text1 setLeftView:paddingView3];
  [_ans6Text1 setLeftViewMode:UITextFieldViewModeAlways];
  UIView *paddingView3new1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
  [_Ans6Text1new1 setClipsToBounds:NO];
  [_Ans6Text1new1.layer setBorderWidth:1.0];
  [_Ans6Text1new1.layer setBorderColor:[UIColor whiteColor].CGColor];
  [_Ans6Text1new1.layer setCornerRadius:4.0];
  [_Ans6Text1new1 setLeftView:paddingView3new1];
  [_Ans6Text1new1 setLeftViewMode:UITextFieldViewModeAlways];

  UIView *paddingView4 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
  [_ans6Text2 setClipsToBounds:NO];
  [_ans6Text2.layer setBorderWidth:1.0];
  [_ans6Text2.layer setBorderColor:[UIColor whiteColor].CGColor];
  [_ans6Text2.layer setCornerRadius:4.0];
  [_ans6Text2 setLeftView:paddingView4];
  [_ans6Text2 setLeftViewMode:UITextFieldViewModeAlways];
  [_ans6Text2 setInputView:_datePicker];
  UIView *paddingView4new1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
  [_Ans6Text2new1 setClipsToBounds:NO];
  [_Ans6Text2new1.layer setBorderWidth:1.0];
  [_Ans6Text2new1.layer setBorderColor:[UIColor whiteColor].CGColor];
  [_Ans6Text2new1.layer setCornerRadius:4.0];
  [_Ans6Text2new1 setLeftView:paddingView4new1];
  [_Ans6Text2new1 setLeftViewMode:UITextFieldViewModeAlways];
  [_Ans6Text2new1 setInputView:_datePicker];

  UIView *paddingView5 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
  [_ans6Text3 setClipsToBounds:NO];
  [_ans6Text3.layer setBorderWidth:1.0];
  [_ans6Text3.layer setBorderColor:[UIColor whiteColor].CGColor];
  [_ans6Text3.layer setCornerRadius:4.0];
  [_ans6Text3 setLeftView:paddingView5];
  [_ans6Text3 setLeftViewMode:UITextFieldViewModeAlways];
  UIView *paddingView5new1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
  [_Ans6Text3new1 setClipsToBounds:NO];
  [_Ans6Text3new1.layer setBorderWidth:1.0];
  [_Ans6Text3new1.layer setBorderColor:[UIColor whiteColor].CGColor];
  [_Ans6Text3new1.layer setCornerRadius:4.0];
  [_Ans6Text3new1 setLeftView:paddingView5new1];
  [_Ans6Text3new1 setLeftViewMode:UITextFieldViewModeAlways];

  UIView *paddingView6 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
  [_ans6Text4 setClipsToBounds:NO];
  [_ans6Text4.layer setBorderWidth:1.0];
  [_ans6Text4.layer setBorderColor:[UIColor whiteColor].CGColor];
  [_ans6Text4.layer setCornerRadius:4.0];
  [_ans6Text4 setLeftView:paddingView6];
  [_ans6Text4 setLeftViewMode:UITextFieldViewModeAlways];
  UIView *paddingView6new1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
  [_Ans6Text4new1 setClipsToBounds:NO];
  [_Ans6Text4new1.layer setBorderWidth:1.0];
  [_Ans6Text4new1.layer setBorderColor:[UIColor whiteColor].CGColor];
  [_Ans6Text4new1.layer setCornerRadius:4.0];
  [_Ans6Text4new1 setLeftView:paddingView6new1];
  [_Ans6Text4new1 setLeftViewMode:UITextFieldViewModeAlways];

Ans6Text1new1,Ans6Text2new1,Ans6Text3new1 and Ans6Text4new1 are the textfields which are not getting selected. This is code for second textfield where we get the date.


-(void)textFieldDidEndEditing:(UITextField *)textField {
  NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  [formatter setDateFormat:@"dd-MM-yyyy"];
  NSString *selectedDate = [formatter stringFromDate:[self.datePicker date]];
  NSString *pastDate = [formatter stringFromDate:[self.datePickerPast date]];

  if ([textField isEqual:_ans6Text2]) {
  [self.ans6Text2 setText:selectedDate];
  [self.ans6Text2 setBackgroundColor:[UIColor whiteColor]];
  [self.ans6Text2 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
  }
  if ([textField isEqual:_Ans6Text2new1]) {
  [self.Ans6Text2new1 setText:selectedDate];
  [self.Ans6Text2new1 setBackgroundColor:[UIColor whiteColor]];
  [self.Ans6Text2new1 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
  }

  else if ([textField isEqual:_ans4Text2]) {
  [self.ans4Text2 setText:pastDate];
  [self.ans4Text2 setBackgroundColor:[UIColor whiteColor]];
  [self.ans4Text2 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
  }
  else if ([textField isEqual:_ans7Text2]) {
  [self.ans7Text2 setText:pastDate];
  [self.ans7Text2 setBackgroundColor:[UIColor whiteColor]];
  [self.ans7Text2 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
  }
  else if ([textField isEqual:_ans7Text4]) {
  [self.ans7Text4 setText:pastDate];
  [self.ans7Text4 setBackgroundColor:[UIColor whiteColor]];
  [self.ans7Text4 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
  }
  else if ([textField isEqual:_ans7Text5]) {
  [self.ans7Text5 setText:pastDate];
  [self.ans7Text5 setBackgroundColor:[UIColor whiteColor]];
  [self.ans7Text5 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
  }
  else if ([textField isEqual:_ans8Text1]) {
  [self.ans8Text1 setText:pastDate];
  [self.ans8Text1 setBackgroundColor:[UIColor whiteColor]];
  [self.ans8Text1 setTextColor:[UIColor colorWithRed:5.0/255.0 green:85.0/255.0 blue:160.0/255.0 alpha:1.0]];
  }
}

This is the code for my Button click in UIView which is also not working

  
 
- (void)add1ChemicalTapped:(UIButton *)sender {  NSLog(@"ADDnew tapped"); }

And in viewDidLoad

  
 
- (void)viewDidLoad {
  @try{
  [super viewDidLoad];

  if(self.isReceived==true){
  [_HSSwitch setEnabled:NO];
  }


  [_Add1Chemical addTarget:self action:@selector(add1ChemicalTapped:) forControlEvents:UIControlEventTouchUpInside];
}
Accepted Answer

This is not a question about a feature of the Objective-C language, so it’s posted in the wrong forum. Cocoa Touch might be better.


There‘s too much going on there for me to follow. Inline images don’t show in these forums, so nobody can see your screenshots. All I can suggest here is to use the view debugger in Xcode. This will give you a wire frame visual representation of your view hierarchy so you can easily see if a view is not sized or positioned correctly. I believe you can also inspect properties of individual views such as userInteractionEnabled. At least you can see the debugDescription for sure.


As you probably know, for a view to be tappable, that view and all of its superviews must have userInteractionEnabled set to YES. And subviews must be “inside” superviews. A common error is to have the superview sized or positioned incorrectly, so the subviews end up being drawn outside the superview’s bounds. clipsToBounds is NO by default, so this is not immediately obvious. The subviews can still be drawn but won’t receive touches.

>screenshot of


As JP aptly notes, sharing of inline images is prohibited. See this link for how to deal with that: For Best Results - Read the Label

UIView inside UIView with TextField and Button not working
 
 
Q