Ios 在UIScrollView中单击按钮,将UITextFields一个接一个地添加到UIScrollView中
我有一个包含多个组件的UIScrollView。我需要以这样的方式添加UITextfields,即点击一个按钮,文本字段将在另一个垂直下方添加一个。例如,如果点击100次按钮,则应添加100个UITextfields,此外,此uitextfield下面还有一些组件,因此uiscrollview应该相应地调整其高度。注意:原始答案使用填充/获取示例代码编辑 下面是一个简单的例子 将视图垂直轴添加到滚动视图。点击每个按钮,创建一个新的UITextField,并将其添加为堆栈视图的arrangedSubview 堆栈视图将自动垂直展开,其约束将自动处理滚动视图的内容大小 新字段将用占位符字符串初始化。 点击填充字段按钮,然后将每个字段的文本设置为样本数据数组中的值。 点击获取文本按钮将把每个字段的文本记录到调试控制台。 ViewController.h ViewController.m 轻敲3下后的结果: 点击足够多需要滚动的按钮后: 点击“填充字段”按钮后:Ios 在UIScrollView中单击按钮,将UITextFields一个接一个地添加到UIScrollView中,ios,objective-c,uiscrollview,uitextfield,Ios,Objective C,Uiscrollview,Uitextfield,我有一个包含多个组件的UIScrollView。我需要以这样的方式添加UITextfields,即点击一个按钮,文本字段将在另一个垂直下方添加一个。例如,如果点击100次按钮,则应添加100个UITextfields,此外,此uitextfield下面还有一些组件,因此uiscrollview应该相应地调整其高度。注意:原始答案使用填充/获取示例代码编辑 下面是一个简单的例子 将视图垂直轴添加到滚动视图。点击每个按钮,创建一个新的UITextField,并将其添加为堆栈视图的arrangedSu
你试过什么?同样,这一个在另一个水平方向上清晰吗?使用UIStackView并将每个文本字段添加为一个排列的子视图。堆栈视图将自动展开,其约束将自动处理滚动视图内容大小。sh_khan。很抱歉,它的verticallyDonMag-非常感谢您的帮助,还需要您的帮助。如何获取每个文本字段的引用?我想在每个文本字段中添加数据,并通过api调用发送它们。好吧,您可以在创建它们时将它们添加到数组中。。。或如果堆栈视图中只有这些元素,请通过_theStackView.arrangedSubviews将它们引用为数组。我需要在每个文本字段中填充数据。我尝试使用数组,但textfield.text始终为零,因为每次单击都会创建新的textfield。因此无法维护数组。@DonMag非常感谢您。您一直是一个saviour@roshan.k-如果这回答了您的问题,请务必将其标记为已接受,以方便其他可能遇到它的用户。
//
// ViewController.h
//
// Created by Don Mag on 12/3/19.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
//
// Created by Don Mag on 12/3/19.
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) UIButton *theButton;
@property (strong, nonatomic) UIButton *fillButton;
@property (strong, nonatomic) UIButton *getButton;
@property (strong, nonatomic) UIScrollView *theScrollView;
@property (strong, nonatomic) UIStackView *theStackView;
@property (strong, nonatomic) NSMutableArray *theData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
// instantiate buttons, scrollView and stackViews
// adds a new text field
_theButton = [UIButton new];
[_theButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[_theButton setTitle:@"Add Field" forState:UIControlStateNormal];
[_theButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[_theButton setBackgroundColor:[UIColor redColor]];
// fills the fields with sample data
_fillButton = [UIButton new];
[_fillButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[_fillButton setTitle:@"Fill Fields" forState:UIControlStateNormal];
[_fillButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[_fillButton setBackgroundColor:[UIColor redColor]];
// logs the text of each field to debug console
_getButton = [UIButton new];
[_getButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[_getButton setTitle:@"Get Text" forState:UIControlStateNormal];
[_getButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[_getButton setBackgroundColor:[UIColor redColor]];
// scroll view will hold the stack view filled with fields
_theScrollView = [UIScrollView new];
[_theScrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_theScrollView setBackgroundColor:[UIColor cyanColor]];
// stack view to hold the new fields
_theStackView = [UIStackView new];
[_theStackView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_theStackView setAxis:UILayoutConstraintAxisVertical];
[_theStackView setSpacing:8.0];
// stack view for buttons
UIStackView *buttonsStackView = [UIStackView new];
[buttonsStackView setTranslatesAutoresizingMaskIntoConstraints:NO];
[buttonsStackView setAxis:UILayoutConstraintAxisHorizontal];
[buttonsStackView setSpacing:8.0];
// add buttons to the buttons stack view
[buttonsStackView addArrangedSubview:_theButton];
[buttonsStackView addArrangedSubview:_fillButton];
[buttonsStackView addArrangedSubview:_getButton];
// add button stack view and scrollView to view
[self.view addSubview:buttonsStackView];
[self.view addSubview:_theScrollView];
// add stackView to scrollView
[_theScrollView addSubview:_theStackView];
UILayoutGuide *g = self.view.safeAreaLayoutGuide;
UILayoutGuide *sg = _theScrollView.contentLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
// constrain buttons 20-pts from top, centered horizontally
[buttonsStackView.topAnchor constraintEqualToAnchor:g.topAnchor constant:20.0],
[buttonsStackView.centerXAnchor constraintEqualToAnchor:g.centerXAnchor constant:0.0],
// constrai scrollView 20-pts below button, 20-pts on each side and bottom
[_theScrollView.topAnchor constraintEqualToAnchor:_theButton.bottomAnchor constant:20.0],
[_theScrollView.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant:-20.0],
[_theScrollView.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:20.0],
[_theScrollView.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:-20.0],
// constrain stackView 8-pts on each side
[_theStackView.topAnchor constraintEqualToAnchor:sg.topAnchor constant:8.0],
[_theStackView.bottomAnchor constraintEqualToAnchor:sg.bottomAnchor constant:-8.0],
[_theStackView.leadingAnchor constraintEqualToAnchor:sg.leadingAnchor constant:8.0],
[_theStackView.trailingAnchor constraintEqualToAnchor:sg.trailingAnchor constant:-8.0],
// constrain stackView width to scrollView frame width minus 16-pts (for 8-pts on each side)
[_theStackView.widthAnchor constraintEqualToAnchor:_theScrollView.frameLayoutGuide.widthAnchor constant:-16.0],
]
];
[_theButton addTarget:self action:@selector(addTextField:) forControlEvents:UIControlEventTouchUpInside];
[_fillButton addTarget:self action:@selector(fillFields:) forControlEvents:UIControlEventTouchUpInside];
[_getButton addTarget:self action:@selector(getFields:) forControlEvents:UIControlEventTouchUpInside];
// sample data to fill the fields with
NSString *s = @"This is just random text that will be used to fill the text fields when the fill button is tapped";
_theData = [s componentsSeparatedByString:@" "].mutableCopy;
}
- (void)fillFields:(id)sender {
// set the text of each field based on the data array
NSInteger idx = 0;
for (UITextField *f in _theStackView.arrangedSubviews) {
// make sure we don't have more fields than our data array
if (idx < _theData.count) {
f.text = _theData[idx++];
}
}
}
- (void)getFields:(id)sender {
// log the text of each field to debug cosole
for (UITextField *f in _theStackView.arrangedSubviews) {
NSLog(@"%@", f.text);
}
}
- (void)addTextField:(id)sender {
// instantiate a new text field
UITextField *f = [UITextField new];
[f setBorderStyle:UITextBorderStyleRoundedRect];
[f setBackgroundColor:[UIColor whiteColor]];
// add it to the stack view
[_theStackView addArrangedSubview:f];
// set its placeholder text (just to make it easy to see what we're doing)
[f setPlaceholder:[NSString stringWithFormat:@"Text Field %ld", _theStackView.arrangedSubviews.count]];
dispatch_async(dispatch_get_main_queue(), ^{
// auto-scroll to bottom so newly added text field is visible
CGRect r = CGRectMake(0.0, self.theScrollView.contentSize.height - 1.0, 1.0, 1.0);
[self.theScrollView scrollRectToVisible:r animated:YES];
});
}
@end