无法将UITextField添加到UIScrollView

时间:2013-10-16 13:15:26

标签: ios uiscrollview uitextfield

我有一个视图控制器,我添加了一个滚动视图。然后根据用户需要的文本字段数量,添加UITextFields的数量,动态调整UIScrollView.contentSize的大小。 到目前为止,我已经检查并仔细检查了委托,contentSize属性的大小,文本字段的来源和大小。我使用以下代码来执行此操作:

-(void)setScrollViewSize
{
    NSLog(@"Rows count: %d", self.rows);
    self.matrixInputScrollView.contentSize = CGSizeZero;
    CGSize scrollVieSize;
    if (!self.columns) {
    NSLog(@"Setting scroll view frame row");
    scrollVieSize = CGSizeMake(self.rows * TEXT_FIELD_WIDTH + (self.rows - 1) * TEXT_FIELD_INDENT, TEXT_FIELD_HEIGHT);
    }
    else
    {
        NSLog(@"Setting scroll view frame rows and columns");
        scrollVieSize = CGSizeMake(self.rows * TEXT_FIELD_WIDTH + (self.rows - 1) * TEXT_FIELD_INDENT, self.columns * TEXT_FIELD_HEIGHT + (self.columns - 1) * TEXT_FIELD_INDENT);
    }
    self.matrixInputScrollView.contentSize = scrollVieSize;

     if (!self.columns) {
         for (NSInteger i = 0; i < self.rows; ++i) {
             NSLog(@"Text filed adding");
             UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(i * (TEXT_FIELD_WIDTH + TEXT_FIELD_INDENT), 0, TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT)];
             NSLog(@"uitextfield position ad size: %f %f %f %f", textField.frame.origin.x, textField.frame.origin.y, textField.frame.size.width, textField.frame.size.height);
             [self.matrixInputScrollView addSubview:textField];
         }
      }
      else
      {
          for (NSInteger i = 0; i < self.rows; ++i) {
             for (NSInteger j = 0; j < self.columns; ++j) {
                  UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(i * (TEXT_FIELD_WIDTH + TEXT_FIELD_INDENT), j * (TEXT_FIELD_HEIGHT + TEXT_FIELD_INDENT), TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT)];
                   [self.matrixInputScrollView addSubview:textField];
            }
         }
      }

    NSLog(@"Before setNeedsDisplay");
    NSLog(@"Scroll view position and size: %f %f %f %f",      self.matrixInputScrollView.frame.origin.x, self.matrixInputScrollView.frame.origin.y,   self.matrixInputScrollView.frame.size.width, self.matrixInputScrollView.frame.size.height);
    NSLog(@"Scroll view content size: %f %f", self.matrixInputScrollView.contentSize.width,    self.matrixInputScrollView.contentSize.height);
}

以上样本的输出如下:

2013-10-16 15:27:30.672 RZCalc[2765:a0b] Rows count: 3
2013-10-16 15:27:30.672 RZCalc[2765:a0b] Setting scroll view frame row
2013-10-16 15:27:30.673 RZCalc[2765:a0b] Text filed adding
2013-10-16 15:27:30.673 RZCalc[2765:a0b] uitextfield position ad size: 0.000000 0.000000 40.000000 30.000000
2013-10-16 15:27:30.674 RZCalc[2765:a0b] Text filed adding
2013-10-16 15:27:30.674 RZCalc[2765:a0b] uitextfield position ad size: 50.000000 0.000000 40.000000 30.000000
2013-10-16 15:27:30.674 RZCalc[2765:a0b] Text filed adding
2013-10-16 15:27:30.675 RZCalc[2765:a0b] uitextfield position ad size: 100.000000 0.000000 40.000000 30.000000
2013-10-16 15:27:30.675 RZCalc[2765:a0b] Before setNeedsDisplay
2013-10-16 15:27:30.676 RZCalc[2765:a0b] Scroll view position and size: 76.000000 42.000000 244.000000 246.000000
2013-10-16 15:27:30.676 RZCalc[2765:a0b] Scroll view content size: 140.000000 30.000000

您在此处看到的是创建并放置在屏幕上的滚动视图,相应地调整了内容大小并创建了3个文本字段并将其添加到滚动视图中,其原点和大小在contentSize限制内。

结果是滚动视图显示在屏幕上,文本字段不显示。

您能否判断上述代码是否有问题,或者您是否需要更多相关信息?

1 个答案:

答案 0 :(得分:1)

添加评论作为答案:

将标签的背景颜色设置为与滚动视图的颜色不同,以查看它们是否存在。您没有为标签设置任何格式选项,因此它们目前可能只是透明视图。