Repository navigation
Expand file tree
/
Copy pathZLCollectionReusableView.m
More file actions
57 lines (47 loc) · 2.28 KB
/
Copy pathZLCollectionReusableView.m
File metadata and controls
57 lines (47 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// ZLCollectionReusableView.m
// ZLCollectionView
//
// Created by zhaoliang chen on 2018/7/9.
// Copyright © 2018年 zhaoliang chen. All rights reserved.
//
#import "ZLCollectionReusableView.h"
#import "ZLCollectionViewLayoutAttributes.h"
@interface ZLCollectionReusableView ()
@property(nonatomic,strong)UIImageView* ivBackground;
@end
@implementation ZLCollectionReusableView
- (instancetype)initWithFrame:(CGRect)frame {
if (self == [super initWithFrame:frame]) {
self.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:self.ivBackground];
self.ivBackground.translatesAutoresizingMaskIntoConstraints = NO;
[self addConstraints:@[
[NSLayoutConstraint constraintWithItem:self.ivBackground attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:self.ivBackground attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant: 0.0],
[NSLayoutConstraint constraintWithItem:self.ivBackground attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant: 0.0],
[NSLayoutConstraint constraintWithItem:self.ivBackground attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant: 0.0]
]];
}
return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
[super applyLayoutAttributes:layoutAttributes];
//设置背景颜色
ZLCollectionViewLayoutAttributes *ecLayoutAttributes = (ZLCollectionViewLayoutAttributes*)layoutAttributes;
if (ecLayoutAttributes.color) {
self.backgroundColor = ecLayoutAttributes.color;
}
if (ecLayoutAttributes.image) {
self.ivBackground.image = ecLayoutAttributes.image;
}
}
- (UIImageView*)ivBackground {
if (!_ivBackground) {
_ivBackground = [[UIImageView alloc]init];
_ivBackground.contentMode = UIViewContentModeScaleAspectFill;
_ivBackground.backgroundColor = [UIColor clearColor];
}
return _ivBackground;
}
@end