@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
@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