更方便的布局方式,9.0以后才能使用

效果图
#import "ViewController.h"
@interface ViewController ()
///stackView
@property (nonatomic,weak) UIStackView * stackView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIStackView * stackView = [[UIStackView alloc] init];
stackView.frame = CGRectMake(0, (self.view.frame.size.height-200)*0.5, self.view.frame.size.width, 200);
[self.view addSubview:stackView];
self.stackView= stackView;
//子视图
UIButton * btn0 = [[UIButton alloc] init];
btn0.backgroundColor = [UIColor yellowColor];
UIButton * btn1 = [[UIButton alloc] init];
btn1.backgroundColor = [UIColor greenColor];
UIButton * btn2 = [[UIButton alloc] init];
btn2.backgroundColor = [UIColor purpleColor];
//子视图间隔
self.stackView.spacing = 2;
//布局方向 (默认横向布局)
self.stackView.axis = UILayoutConstraintAxisHorizontal;
//子视图对齐方式 (枚举值)
self.stackView.alignment = UIStackViewAlignmentFill;
//子视图分部方式 (枚举值)
self.stackView.distribution = UIStackViewDistributionFillEqually;
[self.stackView addArrangedSubview:btn0];
[self.stackView addArrangedSubview:btn1];
[self.stackView addArrangedSubview:btn2];
//添加删除插入子视图方法---
//将子视图,添加到 stackView 的 arrangedSubviews列表中
//[self.stackView addArrangedSubview:label];
//删除 stackView的 arrangedSubviews列表中的 view
//[self.stackViewremoveArrangedSubview:lastLabel];
//插入一个视图到 stackView的 arrangedSubviews列表中
//[self.stackViewinsertArrangedSubview:labelatIndex:2];
}
@end
网友评论