UIStackView 对UIView视图管理

本文介绍如何利用UIStackView有效地管理UIView的子视图,包括视图的添加和缩放操作。通过创建视图并将其存储在数组中,UIStackView能够便捷地布局和调整这些视图的尺寸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIStackView 对UIView子视图进行管理 ,对原片进行缩放

首先,建立一些视图,将视图放在数组中:

 NSMutableArray *array = [[NSMutableArray alloc] init];
    for (int i = 0; i < 4; i ++) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
        [array addObject:view];
    } 

// 初始化方法,通过数组传入被管理的视图
    self.stackView = [[UIStackView alloc] initWithArrangedSubviews:array];
    [self.view addSubview:self.stackView];
    
    self.stackView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 500);
    self.stackView.axis = UILayoutConstraintAxisVertical;   // 垂直方向
    /*
     typedef NS_ENUM(NSInteger, UILayoutConstraintAxis) {
     UILayoutConstraintAxisHorizontal = 0,  水平布局
     UILayoutConstraintAxisVertical = 1     竖直布局
     };
     */
    self.stackView.distribution = UIStackViewDistributionFillEqually;   // 排列方式
    /*
     typedef NS_ENUM(NSInteger, UIStackViewDistribution) {
     UIStackViewDistributionFill = 0,   // 充满,当只有一个控件时可以使用
     UIStackViewDistributionFillEqually,    // 平分充满,每个控件占据相同尺寸排列充满
     UIStackViewDistributionFillProportionally, // 会优先按照约束的尺寸进行排列,如果没有充满,会拉伸最后一个排列的控件充满
     UIStackViewDistributionEqualSpacing,   // 等间距排列
     UIStackViewDistributionEqualCentering, // 中心距离相等
     } NS_ENUM_AVAILABLE_IOS(9_0);
     
     */
    self.stackView.alignment = UIStackViewAlignmentFill;    // 对齐模式
    /*
     typedef NS_ENUM(NSInteger, UIStackViewAlignment) {
     UIStackViewAlignmentFill,  // 水平布局时为高度充满,竖直布局时为宽度充满
     UIStackViewAlignmentLeading,   // 前边对齐
     UIStackViewAlignmentTop = UIStackViewAlignmentLeading, // 顶部对齐
     UIStackViewAlignmentFirstBaseline, // 第一个控件文字的基线对齐 水平布局有效
     UIStackViewAlignmentCenter,    // 中心对齐
     UIStackViewAlignmentTrailing,  // 后边对齐
     UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing, // 底部对齐
     UIStackViewAlignmentLastBaseline,  // 基线对齐,水平布局有效
     } NS_ENUM_AVAILABLE_IOS(9_0);
     
     */
    
    self.stackView.spacing = 5;    // 设置最小间距
    self.stackView.baselineRelativeArrangement = YES;   // 设置布局是是否参照基线
    self.stackView.layoutMarginsRelativeArrangement = NO;  // 设置布局时是否以控件的LayoutMargins为标准,默认为NO,是以控件的bounds为标准
    
    
    
    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(50, self.view.bounds.size.height - 50, 50, 50)];
    [button2 addTarget:self action:@selector(removeView) forControlEvents:(UIControlEventTouchUpInside)];
    [button2 setTitle:@"删除" forState:(UIControlStateNormal)];
    button2.titleLabel.font = [UIFont systemFontOfSize:25];
    [button2 setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)]; 
    [self.view addSubview:button2];
    
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(150, self.view.bounds.size.height - 50, 50, 50)];
    [button1 addTarget:self action:@selector(addView) forControlEvents:(UIControlEventTouchUpInside)];
    [button1 setTitle:@"添加" forState:(UIControlStateNormal)];
    button1.titleLabel.font = [UIFont systemFontOfSize:25];
    [button1 setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [self.view addSubview:button1];
    
    
    UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(250, self.view.bounds.size.height - 50, 50, 50)];
    [button3 addTarget:self action:@selector(insertView) forControlEvents:(UIControlEventTouchUpInside)];
    [button3 setTitle:@"插入" forState:(UIControlStateNormal)];
    button3.titleLabel.font = [UIFont systemFontOfSize:25];
    [button3 setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [self.view addSubview:button3];
    
//响应事件
- (void)addView{
    
    
    UIView *newView = [[UIView alloc] init];
    newView.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    
    [self.stackView addArrangedSubview:newView];
    // 在添加的时候会有动画效果
    [UIView animateWithDuration:1 animations:^{
        [self.stackView layoutIfNeeded];
    }];
}


- (void)removeView{
    
    
    UIView *view = [[self.stackView arrangedSubviews] lastObject];  // 获取被管理的所有视图中的最后一个
    [self.stackView removeArrangedSubview:view];    // 移除一个view
    [UIView animateWithDuration:1 animations:^{
        [self.stackView layoutIfNeeded];
    }];
}

- (void)insertView{
    
    UIView *newView = [[UIView alloc] init];
    newView.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    NSArray *array = [self.stackView arrangedSubviews];
    if (array.count > 2) {
        [self.stackView insertArrangedSubview:newView atIndex:2];   // 插入一个视图
        [UIView animateWithDuration:1 animations:^{
            [self.stackView layoutIfNeeded];
        }];
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值