自動レイアウトを使用して、スクロールビューに「1」ラベル、「1」テキストフィールド、および「2」ボタンを追加しています。
ここでの主な要件は、「Add1」ボタンをクリックしたときに、「textfiled1」とボタンの間に追加のテキストフィールドを 1 つ追加する必要があることです。
「Add2」ボタンをクリックすると、「textfiled2」とボタンの間に別のテキストフィールドを追加する必要があります。
そして、削除ボタンをクリックすると、追加された両方のテキストフィールドを削除する必要があり、そのためにいくつかのコードを書きましたが、これは機能しません。
私のコード:
#import "ViewController2.h"
#import "Masonry.h"
@interface ViewController2 ()
@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic) UILabel *label1;
@property (nonatomic) UITextField *textField1;
@property (nonatomic) UITextField *textField2;
@property (nonatomic) UITextField *textField3;
@property (nonatomic) UIButton *leftButton;
@property (nonatomic) UIButton *rightButton;
@property (nonatomic) UIButton *removeButton;
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
self.scrollView = [[UIScrollView alloc] init];
self.label1 = [[UILabel alloc] init];
self.textField1 = [[UITextField alloc] init];
self.textField2 = [[UITextField alloc] init];
self.textField3 = [[UITextField alloc] init];
self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.removeButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.label1.text = @"Label 1";
self.label1.backgroundColor = [UIColor orangeColor];
self.textField1.placeholder = @"TextField 1";
self.textField1.backgroundColor = [UIColor lightGrayColor];
self.textField2.placeholder = @"TextField 2";
self.textField2.backgroundColor = [UIColor lightGrayColor];
self.textField3.placeholder = @"TextField 3";
self.textField3.backgroundColor = [UIColor lightGrayColor];
self.leftButton.backgroundColor = [UIColor greenColor];
[self.leftButton setTitle:@"Add1" forState:UIControlStateNormal];
[self.leftButton addTarget:self
action:@selector(Adding1)
forControlEvents:UIControlEventTouchUpInside];
self.rightButton.backgroundColor = [UIColor grayColor];
[self.rightButton setTitle:@"Add2" forState:UIControlStateNormal];
[self.rightButton addTarget:self
action:@selector(Adding2)
forControlEvents:UIControlEventTouchUpInside];
self.removeButton.backgroundColor = [UIColor blackColor];
[self.removeButton setTitle:@"Remove" forState:UIControlStateNormal];
[self.removeButton addTarget:self
action:@selector(Remove)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.scrollView];
[self.scrollView addSubview:self.label1];
[self.scrollView addSubview:self.textField1];
[self.scrollView addSubview:self.textField2];
[self.scrollView addSubview:self.textField3];
[self.scrollView addSubview:self.leftButton];
[self.scrollView addSubview:self.rightButton];
[self.scrollView addSubview:self.removeButton];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(30, 10, 10, 10));
}];
[self.label1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@10);
make.left.equalTo(@10);
make.right.equalTo(@-10);
make.centerX.equalTo(self.scrollView);
}];
[self.textField1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.label1.mas_bottom).offset(10);
make.left.equalTo(@10);
make.right.equalTo(@-10);
make.height.equalTo(@30);
}];
[self.textField2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@10);
make.right.equalTo(@-10);
}];
[self Removing1];
[self.textField3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@10);
make.right.equalTo(@-10);
}];
[self Removing2];
//Applying Autolayouts for UIButtons:
[self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField2.mas_bottom).offset(40);
make.left.equalTo(@10);
make.height.equalTo(@30);
make.bottom.equalTo(@-10);
}];
[self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.leftButton);
make.right.equalTo(@-10);
make.left.equalTo(self.leftButton.mas_right).offset(20);
make.height.equalTo(@30);
make.width.equalTo(self.leftButton);
make.bottom.equalTo(@-10);
}];
[self.removeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.leftButton.mas_bottom).offset(40);
make.top.equalTo(self.rightButton.mas_bottom).offset(40);
make.left.equalTo(@10);
make.right.equalTo(@-10);
make.height.equalTo(@30);
make.bottom.equalTo(@-10);
}];
}
-(void)Adding1 {
[self.textField2 mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField1.mas_bottom).offset(10);
make.height.mas_equalTo(30);
}];
}
-(void)Adding2 {
[self.textField2 mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField2.mas_bottom).offset(10);
make.height.mas_equalTo(30);
}];
}
-(void)Remove {
}
-(void)Removing1{
[self.textField2 mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField1.mas_bottom).offset(0);
make.height.mas_equalTo(0);
}];
}
-(void)Removing2{
[self.textField3 mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField2.mas_bottom).offset(0);
make.height.mas_equalTo(0);
}];
}
@end