xcode 带有动态内容的 UIScrollView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22888508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:51:19  来源:igfitidea点击:

UIScrollView with dynamic content

iosobjective-cxcodeuiscrollview

提问by Aleksander

I'm trying to implement a UIScrollView, but every tutorial out there deals with a preset number of items.

我正在尝试实现一个UIScrollView,但是那里的每个教程都涉及预设数量的项目。

What I have are multiple UITextField's, but the number of textfields vary. Basically, as soon as one textFieldcontains text, another empty one appears below it, allowing the user to fill in an unlimited number of textfields.

我有多个UITextField,但文本字段的数量各不相同。基本上,只要一个 textField包含文本,它下面就会出现另一个空的,允许用户填写无限数量的文本字段。

My code creates a new textfield as soon as the user types something into a previous one. The x-coordinatesof this is the same as the previous one, but the y-coordinatesto the new one equals the height of the previous + 5px.

只要用户在前一个文本字段中输入内容,我的代码就会创建一个新的文本字段。这个x-coordinates的和上一个一样,但是y-coordinates新的等于前一个的高度+5px。

I'm using storyboards, and I have placed my original textFieldwithin a UIScrollView. I have connected this scrollViewto my code, and I add a new textfield this way: [self.scrollView addSubview:newTextField];

我正在使用故事板,并将我的原件textField放在UIScrollView. 我已将此连接scrollView到我的代码,并以这种方式添加一个新的文本字段:[self.scrollView addSubview:newTextField];

However, when the amount of textFieldsexceeds the scrollView, I cannot scroll to reveal the new one that's been added.

但是,当数量textFields超过 时scrollView,我无法滚动以显示已添加的新数量。

How would I go about doing this? I don't think I completely get the setContentSizething, so it might have something to do with that. Here are some pictures and code to explain my problem:

我该怎么做呢?我不认为我完全明白这setContentSize件事,所以它可能与此有关。这里有一些图片和代码来解释我的问题:

Code to add new textfield:

添加新文本字段的代码:

UITextField *newTextField = [[UITextField alloc]initWithFrame:CGRectMake(textField.frame.origin.x, textField.frame.origin.y + textField.frame.size.height+5, textField.frame.size.width, textField.frame.size.height)];
newTextField.placeholder = @"Add more text";
newTextField.borderStyle = UITextBorderStyleNone;
newTextField.font = [UIFont systemFontOfSize:14];
newTextField.autocorrectionType = UITextAutocorrectionTypeYes;
newTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
newTextField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
newTextField.returnKeyType = UIReturnKeyDone;
[newTextField setTag:textFieldTag];
newTextField.delegate = self;
[self.scrollView addSubview:newTextField];

Storyboard:

故事板:

Here you can see how I have placed the original textfield within my scrollview

在这里您可以看到我如何将原始文本字段放置在我的滚动视图中

Here you can see how I have placed the original textfield within my scrollview

在这里您可以看到我如何将原始文本字段放置在我的滚动视图中

How it looks in the simulator:

它在模拟器中的外观:

When you enter text in the textfield, this happens:

当您在文本字段中输入文本时,会发生这种情况:

When you enter text in the textfield, this happens:

当您在文本字段中输入文本时,会发生这种情况:

An empty textfield appears below the previous one

上一个文本框下方出现一个空文本框

An empty textfield appears below the previous one.

一个空的文本字段出现在前一个文本字段的下方。

However, when you exceed the ScrollView, this happens

但是,当您超过 ScrollView 时,就会发生这种情况

You can no longer see the new textfield, because it is below the scrollView

您无法再看到新的文本字段,因为它在 scrollView 下方

You can no longer see the new textField because it is below the scrollView's boundaries. You can not scroll to reveal it either.

您无法再看到新的 textField,因为它位于 scrollView 的边界之下。您也无法滚动以显示它。



Does anyone know how to solve this? And if you have time, how would you make it so the scrollview automatically scrolls down to reveal the new textfield that's been added?

有谁知道如何解决这个问题?如果您有时间,您将如何使滚动视图自动向下滚动以显示已添加的新文本字段?

Any help is greatly appreciated! Thanks!

任何帮助是极大的赞赏!谢谢!

回答by Jeffery Thomas

The contentSizeneeds to be the size of content contained within the scroll view.

contentSize需要是包含滚动视图中的内容的大小。

If the contentSizeis wider than the bounds.size, then you can scroll left and right. If the contentSizeis taller than the bounds.size, then you can scroll up and down.

如果contentSize比 宽bounds.size,则您可以左右滚动。如果contentSize高于bounds.size,则您可以上下滚动。

You need to set the contentSizeto be the entire area you wish to contain within your scroll view.

您需要将 设置为contentSize您希望包含在滚动视图中的整个区域。

UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(textField.frame.origin.x, textField.frame.origin.y + textField.frame.size.height+5, textField.frame.size.width, textField.frame.size.height)];
textField.placeholder = @"Add more text";
textField.borderStyle = UITextBorderStyleNone;
textField.font = [UIFont systemFontOfSize:14];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
textField.returnKeyType = UIReturnKeyDone;
textField.tag = textFieldTag;
textField.delegate = self;

[self.scrollView addSubview:textField];

// Update the contentSize to include the new text field.
CGFloat width = self.scrollView.bounds.size.width;
CGFloat height = CGRectGetMaxY(textField.frame);
self.scrollView.contentSize = CGSizeMake(width, height);

NOTES:

笔记:

  • Don't start variables or methods with new. It has special meaning and you will confuse other Objective-C developers and/or the compiler.
  • textField.tag = …is the same as [textfield setTag:…];You seem to like the dot syntax in other places, so I switched to that.
  • I'm assuming you don't want the scroll view to pan left and right, so I pinned the content width to the scroll view's width.
  • 不要以new. 它具有特殊意义,您会混淆其他 Objective-C 开发人员和/或编译器。
  • textField.tag = …就像[textfield setTag:…];你在其他地方似乎喜欢点语法一样,所以我切换到那个。
  • 我假设您不希望滚动视图左右平移,因此我将内容宽度固定到滚动视图的宽度。