//
// ViewController.m
// UIStackView
//
// Created by HarrySun on 16/7/13.
// Copyright © 2016年 Mobby. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIStackView *stackView;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
NSMutableArray *array = [[NSMutableArrayalloc] init];
for (int i =0; i < 4; i ++) {
UIView *view = [[UIViewalloc] init];
view.backgroundColor = [UIColorcolorWithRed:arc4random()%255/255.0green:arc4random()%255/255.0blue:arc4random()%255/255.0alpha:1];
[array addObject:view];
}
// 初始化方法,通过数组传入被管理的视图
self.stackView = [[UIStackViewalloc] initWithArrangedSubviews:array];
[self.viewaddSubview: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 = [[UIButtonalloc] initWithFrame:CGRectMake(50,self.view.bounds.size.height - 50, 50,50)];
[button2 addTarget:selfaction:@selector(removeView)forControlEvents:(UIControlEventTouchUpInside)];
[button2 setTitle:@"删除"forState:(UIControlStateNormal)]; // 设置标题,未选中时状态
button2.titleLabel.font = [UIFontsystemFontOfSize:25]; //设置标题大小
[button2 setTitleColor:[UIColorblueColor] forState:(UIControlStateNormal)];// 设置按钮在某个状态下的标题颜色
[self.viewaddSubview:button2];
UIButton *button1 = [[UIButtonalloc] initWithFrame:CGRectMake(150,self.view.bounds.size.height - 50, 50,50)];
[button1 addTarget:selfaction:@selector(addView)forControlEvents:(UIControlEventTouchUpInside)];
[button1 setTitle:@"添加"forState:(UIControlStateNormal)]; // 设置标题,未选中时状态
button1.titleLabel.font = [UIFontsystemFontOfSize:25]; //设置标题大小
[button1 setTitleColor:[UIColorblueColor] forState:(UIControlStateNormal)];// 设置按钮在某个状态下的标题颜色
[self.viewaddSubview:button1];
UIButton *button3 = [[UIButtonalloc] initWithFrame:CGRectMake(250,self.view.bounds.size.height - 50, 50,50)];
[button3 addTarget:selfaction:@selector(insertView)forControlEvents:(UIControlEventTouchUpInside)];
[button3 setTitle:@"插入"forState:(UIControlStateNormal)]; // 设置标题,未选中时状态
button3.titleLabel.font = [UIFontsystemFontOfSize:25]; //设置标题大小
[button3 setTitleColor:[UIColorblueColor] forState:(UIControlStateNormal)];// 设置按钮在某个状态下的标题颜色
[self.viewaddSubview:button3];
}
- (void)addView{
UIView *newView = [[UIViewalloc] init];
newView.backgroundColor = [UIColorcolorWithRed:arc4random()%255/255.0green:arc4random()%255/255.0blue:arc4random()%255/255.0alpha:1];
[self.stackViewaddArrangedSubview:newView];
// 在添加的时候会有动画效果
[UIViewanimateWithDuration:1animations:^{
[self.stackViewlayoutIfNeeded];
}];
}
- (void)removeView{
UIView *view = [[self.stackViewarrangedSubviews] lastObject]; //获取被管理的所有视图中的最后一个
[self.stackViewremoveArrangedSubview:view]; // 移除一个view
[UIViewanimateWithDuration:1animations:^{
[self.stackViewlayoutIfNeeded];
}];
}
- (void)insertView{
UIView *newView = [[UIViewalloc] init];
newView.backgroundColor = [UIColorcolorWithRed:arc4random()%255/255.0green:arc4random()%255/255.0blue:arc4random()%255/255.0alpha:1];
NSArray *array = [self.stackViewarrangedSubviews];
if (array.count >2) {
[self.stackViewinsertArrangedSubview:newViewatIndex:2]; // 插入一个视图
[UIViewanimateWithDuration:1animations:^{
[self.stackViewlayoutIfNeeded];
}];
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end