当我们点击按钮时如何在 Ios 中设置自动布局?

Posted

技术标签:

【中文标题】当我们点击按钮时如何在 Ios 中设置自动布局?【英文标题】:How to set Auto-layouts in Ios when we tapped on button? 【发布时间】: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 的更新代码 How can I rearrange views when autorotating with autolayout?的可能重复 【参考方案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: 中。没必要。

顺便说一句,尝试通过登录 Xcode 来调试这些 AutoLayout 问题。很有帮助。

【讨论】:

工作正常,非常感谢 user3480295 你真的减少了可能会痛 user3480295 我更新了我的代码,你能解释一下这个问题的解决方案吗【参考方案2】:

滚动视图尚未收到将约束应用于视图的触发器。尝试添加此行以更新约束

[scrollView setNeedsLayout];

【讨论】:

不,那行不通,我已经添加了那行但不工作 当我点击按钮 textview2 添加 textview1 但我想在标签和 textview1 之间添加这个 textview2

以上是关于当我们点击按钮时如何在 Ios 中设置自动布局?的主要内容,如果未能解决你的问题,请参考以下文章

如何在自定义 ListView 中设置按钮的可见性?

iOS 8.0.2 和 iPhone plus 当我在 XIB 中设置 UIButton backgroundImage 时,UIButton 变得更大,尽管这个按钮具有高度和重量的自动布局

代码中设置的自动布局约束未出现在界面生成器中

如何在自动布局中设置视图下方不同数量按钮的宽度

iOS自动布局重置我所有的图像位置

我们如何在 uitableview 单元格中设置标签约束?