阅读 133

UIStackView的使用

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIStackView *horizontalStackView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self.view addSubview:self.horizontalStackView];
    self.horizontalStackView.frame = CGRectMake(10, 100, self.view.frame.size.width-20, 100);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UILabel *lbl = [self textLbl];
    
    [self.horizontalStackView addArrangedSubview:lbl];
    [UIView animateWithDuration:1 animations:^{
        [self.horizontalStackView layoutIfNeeded];
    }];
}

- (UILabel *)textLbl {
    UILabel *lbl = [[UILabel alloc] init];
    lbl.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    lbl.text = @"文字";
    return lbl;
}

- (UIStackView *)horizontalStackView {
    if (!_horizontalStackView) {
        _horizontalStackView = [[UIStackView alloc] init];
        _horizontalStackView.axis = UILayoutConstraintAxisHorizontal;
        _horizontalStackView.distribution = UIStackViewDistributionFillEqually;
        _horizontalStackView.spacing = 10;
        _horizontalStackView.alignment = UIStackViewAlignmentFill;
        _horizontalStackView.backgroundColor = [UIColor yellowColor];
    }
    return _horizontalStackView;
}

@end复制代码

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIStackView *verticalStackView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self.view addSubview:self.verticalStackView];
    self.verticalStackView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UILabel *lbl = [self textLbl];
    
    [self.verticalStackView addArrangedSubview:lbl];
    [UIView animateWithDuration:1 animations:^{
        [self.verticalStackView layoutIfNeeded];
    }];
}

- (UILabel *)textLbl {
    UILabel *lbl = [[UILabel alloc] init];
    lbl.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    lbl.text = @"文字";
    return lbl;
}

- (UIStackView *)verticalStackView {
    if (!_verticalStackView) {
        _verticalStackView = [[UIStackView alloc] init];
        _verticalStackView.axis = UILayoutConstraintAxisVertical;
        _verticalStackView.distribution = UIStackViewDistributionFillEqually;
        _verticalStackView.spacing = 10;
        _verticalStackView.alignment = UIStackViewAlignmentFill;
        _verticalStackView.backgroundColor = [UIColor yellowColor];
    }
    return _verticalStackView;
}

@end复制代码


作者:Silence_xl
链接:https://juejin.cn/post/6930431223336697864


文章分类
后端
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐