Ale*_*der 7 xcode objective-c uiscrollview ios
我正在尝试实现一个UIScrollView,但每个教程都处理预设数量的项目.
我所拥有的是多个UITextField,但文本字段的数量各不相同.基本上,只要一个 textField文本包含文本,就会在其下方显示另一个空文本,允许用户填写无限数量的文本字段.
一旦用户将内容输入到上一个文本中,我的代码就会创建一个新的文本字段.该x-coordinates的,这是一样的前一个,但y-coordinates新一等于以前+ 5px的高度.
我正在使用故事板,而且我把原版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];
Run Code Online (Sandbox Code Playgroud)
故事板:

在这里,您可以看到我如何将原始文本字段放在我的滚动视图中
它在模拟器中的外观如何:

在文本字段中输入文本时,会发生以下情况:

前一个文本框下方会出现一个空文本字段.
但是,当您超过ScrollView时,会发生这种情况

您无法再看到新的textField,因为它位于scrollView的边界下方.你无法滚动显示它.
有谁知道如何解决这个问题?如果你有时间,你会如何做到这一点滚动视图自动向下滚动以显示已添加的新文本字段?
任何帮助是极大的赞赏!谢谢!
的contentSize需要是包含滚动视图中的内容的大小.
如果contentSize宽度大于bounds.size,则可以向左和向右滚动.如果contentSize高于bounds.size,则可以上下滚动.
您需要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);
Run Code Online (Sandbox Code Playgroud)
笔记:
new.它具有特殊含义,您会混淆其他Objective-C开发人员和/或编译器.textField.tag = …就像[textfield setTag:…];你似乎喜欢其他地方的点语法一样,所以我切换到了.| 归档时间: |
|
| 查看次数: |
10788 次 |
| 最近记录: |