【发布时间】:2015-10-14 09:54:53
【问题描述】:
您好,我是自动布局的初学者,在我的项目中我正在使用自动布局,我正在添加一个 label 和两个 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
【问题讨论】:
-
尝试使用
[scrollView layoutIfNeeded];而不是[scrollView setNeedsLayout];。 -
不工作我已经使用那行 [scrollView layoutIfNeeded];
-
你能把
AutoGrowingTextView的代码贴出来吗? -
ok user3480295 查看我发布的关于 AutoGrowingTextView 的更新代码
标签: ios objective-c