具有动态内容的 UIScrollView

时间:2023-02-15
本文介绍了具有动态内容的 UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个 UIScrollView,但那里的每个教程都处理预设数量的项目.

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

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

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.

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

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.

我正在使用情节提要,并且我已将我的原始 textField 放在 UIScrollView 中.我已将此 scrollView 连接到我的代码,并以这种方式添加一个新文本字段:[self.scrollView addSubview:newTextField];

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];

但是,当 textFields 的数量超过 scrollView 时,我无法滚动显示新添加的内容.

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

我该怎么做呢?我认为我没有完全理解 setContentSize 的东西,所以它可能与此有关.下面是一些图片和代码来解释我的问题:

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:

添加新文本字段的代码:

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];

故事板:

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

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

它在模拟器中的样子:

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

一个空的文本域出现在前一个文本域的下方.

An empty textfield appears below the previous one.

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

您无法再看到新的 textField,因为它位于 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.

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

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!

推荐答案

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

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

如果 contentSizebounds.size 宽,那么您可以左右滚动.如果 contentSizebounds.size 高,则可以上下滚动.

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 设置为您希望包含在滚动视图中的整个区域.

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

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);

注意事项:

  • 不要以 new 开头变量或方法.它具有特殊含义,您会混淆其他 Objective-C 开发人员和/或编译器.
  • textField.tag = …[textfield setTag:…]; 好像你喜欢其他地方的点语法,所以我改用那个.
  • 我假设您不希望滚动视图左右平移,所以我将内容宽度固定到滚动视图的宽度.
  • 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.

这篇关于具有动态内容的 UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在 UIScrollView 中嵌套 UIScrollView,以便用户可 下一篇:禁用 UIPageViewController 弹跳 - Swift

相关文章

最新文章