ios - 具有动态内容的 UIScrollView

标签 ios objective-c xcode uiscrollview

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

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

只要用户在前一个文本字段中键入内容,我的代码就会创建一个新的文本字段。这个的 x 坐标与前一个相同,但是新的 y 坐标等于前一个的高度 + 5px。

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

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

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

添加新文本框的代码:

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

在这里您可以看到我是如何将原始文本字段放置在我的 ScrollView 中的

它在模拟器中的样子:

When you enter text in the textfield, this happens:

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

An empty textfield appears below the previous one

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

但是,当你超过ScrollView时,会出现这种情况

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

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


有人知道怎么解决吗?如果您有时间,您会如何让 ScrollView 自动向下滚动以显示已添加的新文本字段?

非常感谢任何帮助!谢谢!

最佳答案

contentSize 需要是 ScrollView 中包含的内容的大小。

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

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

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:…]; 你似乎喜欢其他地方的点语法,所以我改用那个。
  • 我假设您不希望 ScrollView 左右平移,因此我将内容宽度固定为 ScrollView 的宽度。

关于ios - 具有动态内容的 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22888508/

相关文章:

Xcode 无法更新到 6.2 版

xcode - 如何从 info.plist 文件中找到 App Bundle Path?

iOS Pinterest 插入 nil 对象

objective-c - 如果组合框动态添加了新的表格行,那么当 NSComboBox 选择发生更改时,我如何收到通知?

objective-c - 以编程方式触发 "detect displays."

iphone - 在 Cocoa Touch 中获取当前语言环境 24/12 小时样式

Xcode 显示旧的、已删除的 xib 文件

ios - 为什么我的 iPhone 应用程序的网络服务调用仅在 AT&T 4g LTE 上失败?

ios - 用另一种颜色替换图像中的特定颜色

ios - 在不减慢 UI 的情况下在 UITableViewCell 中显示 MKMapView