带有动态内容的UIScrollView [英] UIScrollView with dynamic content

查看:38
本文介绍了带有动态内容的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坐标与上一个相同,但是对新的 y坐标等于前一个+ 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.

有人知道如何解决这个问题吗?如果有时间,您将如何做,以便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!

推荐答案

contentSize 必须是滚动视图中包含的内容的大小.

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

如果 contentSize 宽于 bounds.size ,则可以左右滚动.如果 contentSize 高于 bounds.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆