首页 文章

添加到剪切视图区域时,UITextField不与用户交互

提问于
浏览
0

我在UIScrollView中创建了一个名为 _waypointSubview 的出口的UIView,它也位于另一个UIView中,并且所有3个都是在我的故事板中创建的 . 从这里开始我以编程方式添加UITextFields,一旦添加了超过5个UITextFields,它就会从UIScrollView中剪切掉 . 然后调整UIScrollView和 _waypointSubview 以补偿添加的额外UITextField . 然后,用户可以向下滚动以查看从视图中剪切的所有UITextField . 只有剪切的UITextField不允许编辑 . 我不知道为什么会这样 . 因此,如果我添加其中的10个,我就无法编辑6到10.我尝试以编程方式告诉它使用调用 [waypointTextField setUserInteractionEnabled:YES]; 启用用户交互,但它不起作用 .

enter image description here

有人可以解释为什么我不能编辑这些字段?谢谢 .

- (IBAction)addWaypoint:(id)sender {

UITextField *waypointTextField = [[UITextField alloc] initWithFrame:CGRectMake(xCord, yCord, 250, 40)];
waypointTextField.borderStyle = UITextBorderStyleRoundedRect;
waypointTextField.font = [UIFont systemFontOfSize:18];
waypointTextField.placeholder = @"enter waypoint";
waypointTextField.autocorrectionType = UITextAutocorrectionTypeNo;
waypointTextField.keyboardType = UIKeyboardTypeDefault;
waypointTextField.returnKeyType = UIReturnKeyDone;
waypointTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
waypointTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
waypointTextField.delegate = self;
[_waypointSubview addSubview:waypointTextField];

UITextField *commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(xCord + 300, yCord, 380, 40)];
commentTextField.borderStyle = UITextBorderStyleRoundedRect;
commentTextField.font = [UIFont systemFontOfSize:18];
commentTextField.placeholder = @"enter comment";
commentTextField.autocorrectionType = UITextAutocorrectionTypeNo;
commentTextField.keyboardType = UIKeyboardTypeDefault;
commentTextField.returnKeyType = UIReturnKeyDone;
commentTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
commentTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
commentTextField.delegate = self;
[_waypointSubview addSubview:commentTextField];

// if waypointArray is not initialized yet, create and add textfield
if (waypointArray == nil) {
    waypointArray = [NSMutableArray arrayWithCapacity:1];
    [waypointArray insertObject:waypointTextField atIndex:0];
    commentArray = [NSMutableArray arrayWithCapacity:1];
    [commentArray insertObject:commentTextField atIndex:0];
}
else{
    // check amount of space left in scroll view, and adjust if needed
    if ([waypointArray count] > 4) {
        self.waypointScrollView.contentSize = CGSizeMake(708, 255 + (([waypointArray count] - 4)*50));
        CGRect frame = self.waypointSubview.frame;
        frame.size.height += 50;
        self.waypointSubview.frame = frame;
        //[waypointTextField setUserInteractionEnabled:YES];
    }
    // add new text fields to corresponding arrays
    [waypointArray addObject:waypointTextField];
    [commentArray addObject:commentTextField];
}
yCord += 50; // update the yCoordinate that will be used for the next textField placement
}

2 回答

  • 0

    我假设你需要通过阅读评论将_waypointSubview连接到故事板上的视图控制器 . 为了做到这一点

    • 右键单击storyboard视图控制器上的视图控制器图标

    • 单击并将小圆圈拖动到您要编辑的对象或您尝试从IBAction实施的方法 .

  • 0

    检查文本区域的边界为 -

    UITextField *commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(xCord + 300, yCord, 380, 40)];
    

    如果你观察xcordinate,Iphone的宽度为320点 . 如果你的文本字段宽度为380磅,那么当它的(0,0)从xCord开始(我不知道它的值是多少)300时你怎么能容纳它呢?

    降低xcordinate值并为其背景着色,以检查它是否落入@rdelmar指出的范围内

相关问题