2013-08-16 35 views
1
  • 我有一个ScrollView(来自xib文件),它按预期工作。
  • 在我的ScrollView中,我有一个UIView(比滚动视图稍小一点)。 UIView有userInteractionEnabled = YES
  • 在我的UIView里面是一个以UIView为中心的UITextField。

当文本框的框架位于UIView的顶部时,textField只响应触摸。例如,当我将帧设置为(0,0,280,100)时,文本字段会作出响应。但是有一个框架(0,100,280,100)不起作用。iOS UITextField在UIScrollView没有响应

TextField的高度为100,但它只响应textField的上半部分。

这里是代码“不工作”(在iOS 7 beta 5上开发)。感谢帮助!

page1 = [[UIView alloc] initWithFrame: CGRectMake(5, 5, scrollView.frame.size.width-10,self.scrollView.frame.size.height-10)]; 

userNameInput = [[UITextField alloc]initWithFrame:CGRectMake(6, 6, 280, 100)]; 
[userNameInput setCenter:CGPointMake(page1.frame.size.width/2, page1.frame.size.height/2.5f)]; 
[userNameInput setTextAlignment:NSTextAlignmentCenter]; 
[userNameInput setTextColor:[UIColor whiteColor]]; 

userNameInput.enabled = YES; 
userNameInput.userInteractionEnabled = YES; 
userNameInput.placeholder = @"Enter..."; 
userNameInput.autocorrectionType = UITextAutocorrectionTypeNo; 
userNameInput.keyboardType = UIKeyboardTypeDefault; 
userNameInput.returnKeyType = UIReturnKeyDone; 
userNameInput.clearButtonMode = UITextFieldViewModeWhileEditing; 
userNameInput.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
userNameInput.font = [SystemUtils getSystemFontWithTextSize:30.0f]; 
userNameInput.delegate = self; 
userNameInput.tag = 1; 

page1.userInteractionEnabled = YES; 
[page1 addSubview:userNameInput]; 

[userNameInput setBackgroundColor:[UIColor blueColor]]; 
[page1 setBackgroundColor:[UIColor redColor]]; 
[scrollView setBackgroundColor:[UIColor greenColor]]; 

[self.scrollView addSubview:page1]; 

回答

0

你需要告诉滚动查看其内容的大小是多少(据推测,在这种情况下,你希望它是同第1页的UIView):

[self.scrollView setContentSize:page1.bounds.size]; 
[self.scrollView addSubview:page1]; 
+0

我试过了,但它仍然没有工作。 –

+0

我假设这段代码来自你的'UIViewController'内?这是从哪个方法取得的?你对'page1'对象的分配可能会被设置为你期望的帧大小(即宽度和高度为0.0) – gschandler

+0

是的!该代码位于我的视图控制器的“viewDidLoad”中。我认为框架尺寸是正确的。我为每个元素设置背景颜色(UIScrollView,UIView和UITextField)看起来好像一切都合适。也不重叠。每个矩形都在另一个矩形内。 –

相关问题