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
UIScrollView with dynamic content
提问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 textField
contains 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-coordinates
of this is the same as the previous one, but the y-coordinates
to 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 textField
within a UIScrollView
. I have connected this scrollView
to 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 textFields
exceeds 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 setContentSize
thing, 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
在这里您可以看到我如何将原始文本字段放置在我的滚动视图中
How it looks in the simulator:
它在模拟器中的外观:
When you enter text in the textfield, this happens:
当您在文本字段中输入文本时,会发生这种情况:
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'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 contentSize
needs to be the size of content contained within the scroll view.
的contentSize
需要是包含滚动视图中的内容的大小。
If the contentSize
is wider than the bounds.size
, then you can scroll left and right. If the contentSize
is taller than the bounds.size
, then you can scroll up and down.
如果contentSize
比 宽bounds.size
,则您可以左右滚动。如果contentSize
高于bounds.size
,则您可以上下滚动。
You need to set the contentSize
to 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:…];
你在其他地方似乎喜欢点语法一样,所以我切换到那个。- 我假设您不希望滚动视图左右平移,因此我将内容宽度固定到滚动视图的宽度。