一、继承关系
源码: NS_CLASS_AVAILABLE_IOS(9_0)
@interfaceUIStackView :UIView
继承与UIView,iOS9.0之后可以使用(项目如果最低适配ios8.0,可以通过自定义UIView实现该功能)
常用的创建方式:
UIStackView* stackView = [[UIStackViewalloc]initWithFrame:CGRectMake(0,64,320,200)];
//设置背景颜色无效,间距的空间默认是透明的
stackView.backgroundColor = [UIColor blueColor];
[self.view addSubview:stackView];
//subview之间的行/列间距
stackView.spacing=50;
//轴向平铺方式:水平/竖直方向
stackView.axis= UILayoutConstraintAxisHorizontal;
stackView.alignment= UIStackViewAlignmentFill;
stackView.distribution= UIStackViewDistributionFillEqually;
UIButton* leftButtonView = [[UIButtonalloc]init];
leftButtonView.backgroundColor= [UIColorgreenColor];
leftButtonView.tag=StackViewBaseTag+0;
[leftButtonView setTitle:@"UIButton0" forState:UIControlStateNormal];
[leftButtonView addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[stackView addArrangedSubview:leftButtonView];
UIButton* rightButtonView = [[UIButtonalloc]init];
rightButtonView.backgroundColor= [UIColoryellowColor];
[rightButtonView addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
rightButtonView.tag=StackViewBaseTag+1;
[rightButtonView setTitle:@"UIButton1" forState:UIControlStateNormal];
//同步执行
[stackView addArrangedSubview:rightButtonView];
//arrangedSubviews数组中的元素顺序决定了subview沿轴向排列的顺序
//同步执行,内部实现 [subview removeFromSuperview]
[_stackView removeArrangedSubview:rightButtonView];
}
-(void)buttonClick:(UIButton*)sender{
NSIntegerbuttonIndex = sender.tag-StackViewBaseTag;
NSLog(@"第%ld个按钮被点击了",(long)buttonIndex);
}