我正在尝试在 UIScrollView 中添加 UITextField。我无法在 UIScrollView 中获取 UITextField,但单击 TextField 什么也不做。我已经尝试了很多东西。我尝试的最后一件事是制作自定义 ScrollView 并检查 hitTest。我将它设置为对 delaysContentTouches 说“不”,但它仍然什么都不做。我在 TextField 上设置了 userInteractionEnabled ,但仍然没有。我看过所有其他类似的问题,但没有一个有答案。
下面的代码显示了我尝试将 Label 和 TextField 添加到 UIView,然后将该 UIView 添加到 ScrollView 中。这一切都在视觉上起作用,但 TextField 什么也不做。
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
lbl.text = @"Sample";
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor blackColor]];
UITextField *txt = [[UITextField alloc] initWithFrame:CGRectMake(115, 0, 195, 30)];
txt.borderStyle = UITextBorderStyleRoundedRect;
txt.font = [UIFont systemFontOfSize:15];
//txt.placeholder = @"enter username";
txt.autocorrectionType = UITextAutocorrectionTypeNo;
txt.keyboardType = UIKeyboardTypeDefault;
txt.returnKeyType = UIReturnKeyNext;
txt.clearButtonMode = UITextFieldViewModeWhileEditing;
[txt canBecomeFirstResponder];
txt.delegate=self;
txt.userInteractionEnabled=TRUE;
[txt addTarget:self action:@selector(tfTouch:) forControlEvents:UIControlEventTouchUpInside];
txt.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
UIView *theView = [[UIView alloc] init];
theView.userInteractionEnabled=TRUE;
[theView addSubview:lbl];
[theView addSubview:txt];
[scrollView addSubview:theView];