1 |
//
|
|
2 |
// ChatLayout
|
|
3 |
// ContainerCollectionReusableView.swift
|
|
4 |
// https://github.com/ekazaev/ChatLayout
|
|
5 |
//
|
|
6 |
// Created by Eugene Kazaev in 2020-2021.
|
|
7 |
// Distributed under the MIT license.
|
|
8 |
//
|
|
9 |
|
|
10 |
import Foundation
|
|
11 |
import UIKit
|
|
12 |
|
|
13 |
/// A container `UICollectionReusableView` that constraints its contained view to its margins.
|
|
14 |
public final class ContainerCollectionReusableView<CustomView: UIView>: UICollectionReusableView {
|
|
15 |
|
|
16 |
/// Default reuse identifier is set with the class name.
|
|
17 |
public static var reuseIdentifier: String {
|
! |
18 |
return String(describing: self)
|
! |
19 |
}
|
! |
20 |
|
|
21 |
/// Contained view.
|
|
22 |
public lazy var customView = CustomView(frame: bounds)
|
|
23 |
|
|
24 |
/// An instance of `ContainerCollectionViewCellDelegate`
|
|
25 |
public weak var delegate: ContainerCollectionViewCellDelegate?
|
|
26 |
|
|
27 |
/// Initializes and returns a newly allocated view object with the specified frame rectangle.
|
|
28 |
/// - Parameter frame: The frame rectangle for the view, measured in points. The origin of the frame is relative
|
|
29 |
/// to the superview in which you plan to add it.
|
|
30 |
override init(frame: CGRect) {
|
! |
31 |
super.init(frame: frame)
|
! |
32 |
setupSubviews()
|
! |
33 |
}
|
! |
34 |
|
|
35 |
@available(*, unavailable, message: "Use init(frame:) instead")
|
|
36 |
/// This constructor is unavailable.
|
|
37 |
public required init?(coder aDecoder: NSCoder) {
|
! |
38 |
fatalError("init(coder:) has not been implemented")
|
! |
39 |
}
|
! |
40 |
|
|
41 |
/// Performs any clean up necessary to prepare the view for use again.
|
|
42 |
public override func prepareForReuse() {
|
! |
43 |
super.prepareForReuse()
|
! |
44 |
delegate?.prepareForReuse()
|
! |
45 |
}
|
! |
46 |
|
|
47 |
/// Gives the cell a chance to modify the attributes provided by the layout object.
|
|
48 |
/// - Parameter layoutAttributes: The attributes provided by the layout object. These attributes represent the values that the layout intends to apply to the cell.
|
|
49 |
/// - Returns: Modified `UICollectionViewLayoutAttributes`
|
|
50 |
public override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
|
! |
51 |
guard let chatLayoutAttributes = layoutAttributes as? ChatLayoutAttributes else {
|
! |
52 |
return super.preferredLayoutAttributesFitting(layoutAttributes)
|
! |
53 |
}
|
! |
54 |
delegate?.apply(chatLayoutAttributes)
|
! |
55 |
|
! |
56 |
let resultingLayoutAttributes: ChatLayoutAttributes
|
! |
57 |
if let preferredLayoutAttributes = delegate?.preferredLayoutAttributesFitting(chatLayoutAttributes) {
|
! |
58 |
resultingLayoutAttributes = preferredLayoutAttributes
|
! |
59 |
} else if let chatLayoutAttributes = super.preferredLayoutAttributesFitting(chatLayoutAttributes) as? ChatLayoutAttributes {
|
! |
60 |
delegate?.modifyPreferredLayoutAttributesFitting(chatLayoutAttributes)
|
! |
61 |
resultingLayoutAttributes = chatLayoutAttributes
|
! |
62 |
} else {
|
! |
63 |
resultingLayoutAttributes = chatLayoutAttributes
|
! |
64 |
}
|
! |
65 |
return resultingLayoutAttributes
|
! |
66 |
}
|
! |
67 |
|
|
68 |
/// Applies the specified layout attributes to the view.
|
|
69 |
/// - Parameter layoutAttributes: The layout attributes to apply.
|
|
70 |
public override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
|
! |
71 |
guard let chatLayoutAttributes = layoutAttributes as? ChatLayoutAttributes else {
|
! |
72 |
return
|
! |
73 |
}
|
! |
74 |
super.apply(layoutAttributes)
|
! |
75 |
delegate?.apply(chatLayoutAttributes)
|
! |
76 |
}
|
! |
77 |
|
|
78 |
private func setupSubviews() {
|
! |
79 |
addSubview(customView)
|
! |
80 |
insetsLayoutMarginsFromSafeArea = false
|
! |
81 |
layoutMargins = .zero
|
! |
82 |
|
! |
83 |
customView.translatesAutoresizingMaskIntoConstraints = false
|
! |
84 |
customView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
|
! |
85 |
customView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor).isActive = true
|
! |
86 |
customView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
|
! |
87 |
customView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor).isActive = true
|
! |
88 |
}
|
! |
89 |
|
|
90 |
}
|
|