Fixed size of image grid
This commit is contained in:
parent
dba29aa2d7
commit
7b86f528b8
@ -30,9 +30,8 @@ class ImageCell: UICollectionViewCell {
|
|||||||
|
|
||||||
func configure(with image: String) {
|
func configure(with image: String) {
|
||||||
guard let url = URL(string: image) else { return }
|
guard let url = URL(string: image) else { return }
|
||||||
print("=== Configure cell with url: \(image)")
|
|
||||||
self.imgView.kf.setImage(with: url, options: [
|
self.imgView.kf.setImage(with: url, options: [
|
||||||
.processor(DownsamplingImageProcessor(size: self.imgView.frame.size)),
|
.processor(DownsamplingImageProcessor(size: self.frame.size)),
|
||||||
.scaleFactor(UIScreen.main.scale),
|
.scaleFactor(UIScreen.main.scale),
|
||||||
.cacheOriginalImage
|
.cacheOriginalImage
|
||||||
])
|
])
|
||||||
@ -45,6 +44,7 @@ class ImageGrid: UICollectionView, UICollectionViewDataSource, UICollectionViewD
|
|||||||
private var spacing: CGFloat = 0
|
private var spacing: CGFloat = 0
|
||||||
|
|
||||||
var selectionChanged: ((Int) -> Void)?
|
var selectionChanged: ((Int) -> Void)?
|
||||||
|
var preferredMaxLayoutWidth: CGFloat = 0
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
super.init(coder: coder)
|
super.init(coder: coder)
|
||||||
@ -71,7 +71,17 @@ class ImageGrid: UICollectionView, UICollectionViewDataSource, UICollectionViewD
|
|||||||
}
|
}
|
||||||
|
|
||||||
override var intrinsicContentSize: CGSize {
|
override var intrinsicContentSize: CGSize {
|
||||||
return self.collectionViewLayout.collectionViewContentSize
|
let size = self.collectionViewLayout.collectionViewContentSize
|
||||||
|
if size == .zero && self.preferredMaxLayoutWidth != 0 {
|
||||||
|
let cellSize: CGFloat = (self.preferredMaxLayoutWidth - self.spacing*(CGFloat(self.columnsCount) - 1))/CGFloat(self.columnsCount)
|
||||||
|
let rows: Int = self.images.count/self.columnsCount + (self.images.count % self.columnsCount == 0 ? 0 : 1)
|
||||||
|
let height = cellSize*CGFloat(rows) + CGFloat(rows - 1)*self.spacing
|
||||||
|
print("intrinsicContentSize: \(CGSize(width: self.preferredMaxLayoutWidth, height: height))")
|
||||||
|
return CGSize(width: self.preferredMaxLayoutWidth, height: height)
|
||||||
|
}
|
||||||
|
|
||||||
|
print("intrinsicContentSize: \(size)")
|
||||||
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
func set(images: [String]) {
|
func set(images: [String]) {
|
||||||
|
|||||||
@ -6,7 +6,7 @@ final class ImageGridCell: Cell<[String]>, CellType {
|
|||||||
|
|
||||||
required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
||||||
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
||||||
|
|
||||||
self.grid = ImageGrid(columns: 3, spacing: 2)
|
self.grid = ImageGrid(columns: 3, spacing: 2)
|
||||||
self.grid.translatesAutoresizingMaskIntoConstraints = false
|
self.grid.translatesAutoresizingMaskIntoConstraints = false
|
||||||
self.contentView.addSubview(self.grid)
|
self.contentView.addSubview(self.grid)
|
||||||
@ -16,12 +16,19 @@ final class ImageGridCell: Cell<[String]>, CellType {
|
|||||||
self.grid.topAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.topAnchor),
|
self.grid.topAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.topAnchor),
|
||||||
self.grid.bottomAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.bottomAnchor)
|
self.grid.bottomAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.bottomAnchor)
|
||||||
])
|
])
|
||||||
|
self.height = { UITableView.automaticDimension }
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder aDecoder: NSCoder) {
|
required init?(coder aDecoder: NSCoder) {
|
||||||
super.init(coder: aDecoder)
|
super.init(coder: aDecoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority,
|
||||||
|
verticalFittingPriority: UILayoutPriority) -> CGSize {
|
||||||
|
self.grid.preferredMaxLayoutWidth = self.bounds.size.width - self.contentView.layoutMargins.left - self.contentView.layoutMargins.right
|
||||||
|
return super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
|
||||||
|
}
|
||||||
|
|
||||||
override func setup() {
|
override func setup() {
|
||||||
super.setup()
|
super.setup()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user