簡體   English   中英

當我們點擊按鈕時,如何在Ios中設置自動布局?

[英]How to set Auto-layouts in Ios when we tapped on button?

嗨,我是自動布局的初學者,在我的項目中,我使用自動布局,並在主滾動視圖上添加了一個標簽 ,兩個textview和兩個按鈕 ,因此一切正常。

這是我的主要要求,當我單擊button1時,必須在label和textview1之間添加“ textview2 ”,如下圖所示。 好的,使用下面的代碼很好。

但在這里我主要要求是當我點擊按鈕2 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 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問題。 有幫助

滾動視圖尚未收到將約束應用於視圖的觸發器。 嘗試添加此行以更新約束

[scrollView setNeedsLayout];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM