无法在UIScrollView中访问UITextField

时间:2012-06-09 15:04:31

标签: iphone uiscrollview uitextfield

我正在尝试在UIScrollView中添加UITextField。我无法在UIScrollView中获取UITextField,但单击TextField不会执行任何操作。我尝试过很多东西。我尝试的最后一件事是制作自定义ScrollView并检查hitTest。我把它设置为在LiveFContentTouches上说“NO”,但它仍然没有做任何事情。我在TextField上设置了userInteractionEnabled但仍然没有。我查看了所有其他类似的问题,但没有一个有答案。

下面的代码显示我尝试将Label和TextField添加到UIView,然后在ScrollView中添加该UIView。这一切都在视觉上有效,但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]; 

1 个答案:

答案 0 :(得分:3)

为什么你有另一种观点,你可以添加你的文本字段&直接在scrollview上标记。可能你只需要为你的视图设置一个框架。应该这样做:

[theView sizeToFit];

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