我有一个视图控制器,我向其中添加了一个滚动视图。然后我添加 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 限制内。
结果是滚动视图显示在屏幕上,而文本字段不显示。
您能否判断上述代码是否有问题,或者您是否需要有关它的更多信息?