您好我是自动布局的初学者,在我的项目中,我使用的是自动布局,我正在添加一个标签和两个 textviews 和两个按钮主滚动视图,所以一切正常。
这里我的主要要求是当我点击button1然后必须在标签和 textview1 之间添加“ textview2 ”,如下图所示。好吧,使用我的下面代码就可以了。
但是这里我的主要要求是当我点击button2 textview2 时,标签和textview1之间被移除了,设计看起来就像下面的图片我已经在“buttonClicked2”中编写了一些代码,但是textview2不是删除我在这里做错了什么?
我的代码:
@interface ViewController7 ()
{
UIScrollView * scrollView;
UILabel * label1;
AutoGrowingTextView * textview1;
AutoGrowingTextView * textview2;
UIButton * button1;
UIButton * button2;
NSDictionary * views;
NSArray * labelConstraint;
NSArray * constraints1;
NSArray * horizontalconstraints;
NSArray * verticalconstraints;
}
@end
@implementation ViewController7
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc]init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:scrollView];
label1 = [[UILabel alloc] init];
label1.text = @"MD(Medician)";
label1.backgroundColor = [UIColor orangeColor];
label1.textAlignment = NSTextAlignmentCenter;
label1.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:label1];
textview1 = [[AutoGrowingTextView alloc] init];
textview1.backgroundColor = [UIColor greenColor];
textview1.textColor = [UIColor whiteColor];
textview1.text = @"While de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figure";
textview1.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:textview1];
textview2 = [[AutoGrowingTextView alloc] init];
textview2.backgroundColor = [UIColor blueColor];
textview2.textColor = [UIColor whiteColor];
textview2.text = @"While de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figureWhile de Villiers was at the crease, South Africa appeared to be on target to postWhile de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figureWhile de Villiers was at the crease, South Africa appeared to be on target to postWhile de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figureWhile de Villiers was at the crease, South Africa appeared to be on target to postWhile de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figureWhile de Villiers was at the crease, South Africa appeared to be on target to postWhile de Villiers was at the crease, South Africa appeared to be on target to post a 300 plus total but his departure left Farhaan Behardien to get them close to that figureWhile de Villiers was at the crease, South Africa appeared to be on target to post";
textview2.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:textview2];
button1 = [[UIButton alloc] init];
[button1 addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:@"Login" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor orangeColor];
button1.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:button1];
button2 = [[UIButton alloc] init];
[button2 addTarget:self action:@selector(buttonClicked2:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:@"Reset" forState:UIControlStateNormal];
button2.backgroundColor = [UIColor blackColor];
button2.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:button2];
[self applyingConstraints];
}
-(void)applyingConstraints
{
//Applying autolayouts
views = NSDictionaryOfVariableBindings(scrollView,label1,textview1,button1,button2,textview2);
NSArray * horizontalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[scrollView]-0-|" options:0 metrics:nil views:views];
NSArray * verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[scrollView]-0-|"options:0 metrics:nil views:views];
[self.view addConstraints:horizontalConstraint];
[self.view addConstraints:verticalConstraint];
//Applying autolayouts for UIlabel
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:label1
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[label1]-10-|"
options:0
metrics:nil
views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[textview1]-10-|"
options:0
metrics:nil
views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[button1(50)]-10-[button2]-10-|"
options:0
metrics:nil
views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[textview1]-10-[button2(30)]-20-|"
options:0
metrics:nil
views:views]];
constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview1]-10-[button1(30)]-20-|"
options:0
metrics:nil
views:views];
[scrollView addConstraints:constraints1];
}
- (void)buttonClicked1 :(id)sender{
[scrollView removeConstraints:constraints1];
horizontalconstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[textview2]-10-|"
options:0
metrics:nil
views:views];
[scrollView addConstraints:horizontalConstraints];
verticalconstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview2]-10-[textview1]-30-[button1(30)]-20-|"
options:0
metrics:nil
views:views];
[scrollView addConstraints:verticalconstraints];
[scrollView setNeedsLayout];
}
- (void)buttonClicked2 :(id)sender{
[scrollView removeConstraints:horizontalConstraints];
[scrollView removeConstraints:verticalconstraints];
constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview1]-10-[button1(30)]-20-|"
options:0
metrics:nil
views:views];
[scrollView addConstraints:constraints1];
[scrollView setNeedsLayout];
}
@end
答案 0 :(得分:1)
<强>更新强>
变化
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[textview1]-10-[button2(30)]-20-|"
options:0
metrics:nil
views:views]];
constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview1]-10-[button1(30)]-20-|"
options:0
metrics:nil
views:views];
到
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual
toItem:button2 attribute:NSLayoutAttributeCenterY multiplier:1.f constant:0]];
constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview2(0)]-[textview1]-10-[button1(30)]-20-|"
options:0
metrics:nil
views:views];
并删除
constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[label1(30)]-10-[textview1]-10-[button1(30)]-20-|"
options:0
metrics:nil
views:views];
方法buttonClicked2:
中的。这是不必要的。
BTW,尝试使用登录Xcode调试这些AutoLayout问题。这很有帮助。
答案 1 :(得分:0)
scrollview尚未收到将约束应用于视图的触发器。尝试添加此行以更新约束
[scrollView setNeedsLayout];