使用自动布局我正在添加" 1"标签和" 1" textfield和" 2"滚动视图上的按钮。
这里我的主要要求是点击" Add1"按钮然后必须在" textfiled1"之间添加一个额外的文本字段。和按钮。
当我点击" 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
答案 0 :(得分:0)
您需要将约束声明为属性。
// in public/private interface
@property (nonatomic, strong) MASConstraint *topConstraint;
为view1添加顶部约束
// when making constraints
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);
make.left.equalTo(superview.mas_left).with.offset(padding.left);
}];
以后要解除约束,你可以简单地调用
// then later you can call
[self.topConstraint uninstall];