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

时间:2014-10-21 05:29:38

标签: ios objective-c cocoa-touch uiview uitextfield

我在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 :(得分:0)

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

  1. 右键单击情景板视图控制器上的视图控制器图标

  2. 点击小圆并将其拖动到您要编辑的对象或您尝试从IBAction实施的方法。

答案 1 :(得分:0)

检查文本字段的边界为 -

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

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

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