1

我正在尝试在 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]; 
4

1 回答 1

3

为什么你有另一个视图,你可以直接在滚动视图上添加你的文本字段和标签。可能您只需要为您的视图设置一个框架。那应该这样做:

[theView sizeToFit];

还要正确设置您的 contentSize。scrollView.contentSize = theView.bounds;

于 2012-06-09T15:13:04.007 回答