static const CGFloat ITEM_SIZE = 32
static const NSUInteger ITEM_COUNT = 4
@interface ViewController ()
@property (strong, nonatomic) UIView *containerView1
@property (strong, nonatomic) UIView *containerView2
@property (assign, nonatomic) CGFloat maxWidth
@property (strong, nonatomic) NSArray *imageNames
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]
[self.view addSubview:self.containerView1]
[self.view addSubview:self.containerView2]
_imageNames = @[@"bluefaces_1", @"bluefaces_2", @"bluefaces_3", @"bluefaces_4"]
[self initContainer1]
[self initContainer2]
}
// 利用透明等宽度的SpaceView实现等间距
- (void)initContainer1 {
UIView *lastSpaceView = [UIView new]
lastSpaceView.backgroundColor = [UIColor greenColor]
[_containerView1 addSubview:lastSpaceView]
[lastSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.mas_equalTo(_containerView1);
}]
for (NSUInteger i = 0
UIView *itemView = [self getItemViewWithIndex:i]
[_containerView1 addSubview:itemView]
[itemView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.width.mas_equalTo(@(ITEM_SIZE));
make.left.mas_equalTo(lastSpaceView.mas_right);
make.centerY.mas_equalTo(_containerView1.mas_centerY);
}]
UIView *spaceView = [UIView new]
spaceView.backgroundColor = [UIColor greenColor]
[_containerView1 addSubview:spaceView]
[spaceView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(itemView.mas_right).with.priorityHigh(); // 降低优先级,防止宽度不够出现约束冲突
make.top.bottom.mas_equalTo(_containerView1);
make.width.mas_equalTo(lastSpaceView.mas_width);
}]
lastSpaceView = spaceView
}
[lastSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(_containerView1.mas_right);
}]
}
// 直接设置multiplier实现等间距
- (void)initContainer2 {
for (NSUInteger i = 0
UIView *itemView = [self getItemViewWithIndex:i]
[_containerView2 addSubview:itemView]
[itemView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(ITEM_SIZE);
make.centerY.mas_equalTo(_containerView2.mas_centerY);
make.centerX.mas_equalTo(_containerView2.mas_right).multipliedBy(((CGFloat)i + 1) / ((CGFloat)ITEM_COUNT + 1));
}]
}
}
- (UIView *)getItemViewWithIndex:(NSUInteger)index {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_imageNames[index % _imageNames.count]]]
imageView.contentMode = UIViewContentModeScaleAspectFit
return imageView
}
- (UIView *)containerView1 {
if (!_containerView1) {
_containerView1 = [[UIView alloc] init]
_containerView1.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1]
_containerView1.frame = CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 40)
}
return _containerView1
}
- (UIView *)containerView2 {
if (!_containerView2) {
_containerView2 = [[UIView alloc] init]
_containerView2.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1]
_containerView2.frame = CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width, 40)
}
return _containerView2
}
@end