【问题标题】:Can't Access UITextField in UIScrollView无法在 UIScrollView 中访问 UITextField
【发布时间】:2012-06-09 15:04:31
【问题描述】:

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

【问题讨论】:

  • 你想要什么????你想只在滚动视图上添加一个文本字段吗?

标签: iphone uiscrollview uitextfield


【解决方案1】:

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

[theView sizeToFit];

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

【讨论】:

  • 我的最终目标是让一个带有标签和文本字段的 UIView 根据需要动态重复多次。我认为做到这一点并让它们排列整齐的最佳方法是将它们放在 UIView 中,然后垂直串行组织 UIView。
  • 你是说因为 UIView 的大小合适,所以即使我能看到它,我也无法触摸 TextField? (我不是在质疑你,只是确保我理解)。
  • 是的。视图只能在其范围内进行接触。如果您打开theView.clipsToBounds,您也不会再看到您的其他视图。 ;)
  • 刚刚回到我的电脑上。你提供的信息帮助很大。我缺少的是 UIView 的初始化。一旦我将其更改为 initWithFrame ,那么一切正常。由于某种原因,适合的尺寸不起作用,但这绝对是问题所在。我将 UIView 背景设为红色,但一无所获。我更改为 initWithFrame 并且背景显示正确,然后触摸事件再次开始工作。再次感谢 jaydee3。
  • 欢迎您。不要忘记接受答案:meta.stackexchange.com/questions/5234/…
猜你喜欢
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-26
  • 2011-05-21
相关资源
最近更新 更多