UIScrollView中的iOS UITextField没有响应

标签 ios uiscrollview uitextfield

  • 我有一个 ScrollView(来自 xib 文件),它按预期工作。
  • 在我的 ScrollView 中,我有一个 UIView(比 ScrollView 小一点)。 UIView 有 userInteractionEnabled = YES
  • 我的 UIView 内部是一个 UITextField,它位于 UIView 的中间。

  • textField 仅在 textfield 的框架位于 UIView 顶部时才响应触摸。例如,当我将框架设置为 (0,0,280,100) 时,文本字段会响应。但是对于一个框架(0,100,280,100)它不起作用。

    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];
    

    最佳答案

    您需要告诉 ScrollView 它的内容大小是多少(大概在这种情况下,您希望它与 page1 UIView 相同):

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

    关于UIScrollView中的iOS UITextField没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18279784/

    相关文章:

    iOS webview height pin(xamarin 或代码)

    ios - 如何将 UIScrollView 的转换应用于 UIView?

    ios - 以编程方式布置 UIScrollView,并为其 subview 添加自动布局,但它不滚动

    iOS UITextFlied secureTextEntry 不适用于 iOS 6.1

    ios - UITextField 自动完成?

    IOS - 从 IOS 应用程序中的另一个类写入 UITextField

    ios - 顶层扩展的非模块化 header 错误

    ios - 使用仪器解决低内存警告

    ios - 使用来自 Parse.com PFQueryTableViewController 的 editActionsForRowAtIndexPath 提取行数据

    iphone - 在 iPhone 上加载超过 10 张图像?