Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom UIContentConfiguration
import UIKit
struct WeatherInfo{
let title: String
let subTitle: String
let iconName: String
let description: String
}
class ViewController: UIViewController {
private var weatherCollectionView: UICollectionView? = nil
override func viewDidLoad() {
super.viewDidLoad()
let squareLayout = UICollectionViewFlowLayout()
squareLayout.itemSize = CGSize(width: 300, height: 150)
squareLayout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: squareLayout)
collectionView.register(UICollectionViewListCell.self, forCellWithReuseIdentifier: "cell")
collectionView.dataSource = self
view.addSubview(collectionView)
weatherCollectionView = collectionView
}
private var sampleData: [WeatherInfo]{
let a = WeatherInfo(
title: "横浜",
subTitle: "神奈川",
iconName: "sun.max",
description: "きょうの関東地方は、冬型の気圧配置と強い寒気の影響で、平野部を中心に晴れるでしょう。北部の山沿いや山間部では雪が降る見込みです。")
let b = WeatherInfo(
title: "松本",
subTitle: "長野",
iconName: "cloud.rain",
description: "長野県は、曇りや晴れで、雪の降っている所があります。")
let c = WeatherInfo(
title: "仙台",
subTitle: "宮城",
iconName: "cloud",
description: "低気圧が日本海にあって、ゆっくり北東へ進んでいます。また、別の低気圧が三陸沖にあって北東へ進んでいます。")
let d = WeatherInfo(
title: "旭川",
subTitle: "北海道",
iconName: "snowflake",
description: "北海道付近は、発達した低気圧が日本海に停滞するでしょう。")
let e = WeatherInfo(
title: "高松",
subTitle: "香川",
iconName: "cloud.sun",
description: "香川県は、高気圧に覆われて晴れていますが、西部では寒気や湿った空気の影響で概ね曇りとなり、雨や雪の降っている所があります。")
return [a,b,c,d,e]
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return sampleData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UICollectionViewListCell
let info = sampleData[indexPath.row]
var config = LargeImageConfig()
config.mainImage = UIImage(systemName: info.iconName)
config.mainTitle = info.title
config.subTitle = info.subTitle
config.detail = info.description
cell.contentConfiguration = config
return cell
}
}
struct LargeImageConfig: UIContentConfiguration {
var mainImage: UIImage? = nil
var mainTitle: String = ""
var subTitle: String = ""
var detail: String = ""
func makeContentView() -> UIView & UIContentView {
return LargeImageContentView(configuration:self)
}
func updated(for state: UIConfigurationState) -> LargeImageConfig {
return self
}
}
class LargeImageContentView : UIView, UIContentView {
private var mainImageView: UIImageView = UIImageView(frame: CGRect.zero)
private var mainTitle: UILabel = UILabel()
private var subTitle: UILabel = UILabel()
private var detail: UILabel = UILabel()
var configuration: UIContentConfiguration{
didSet{
updateConfig()
}
}
init(configuration: UIContentConfiguration) {
self.configuration = configuration
super.init(frame:.zero)
addSubview(mainImageView)
mainImageView.frame = CGRect(x: 220, y: 0, width: 80, height: 80)
mainImageView.layer.borderColor = UIColor.lightGray.cgColor
mainImageView.layer.borderWidth = 1.0
mainImageView.layer.cornerRadius = 5.0
mainImageView.contentMode = .scaleAspectFit
mainTitle.frame = CGRect(x: 0, y: 0, width: 300, height: 40)
mainTitle.font = UIFont.boldSystemFont(ofSize: 30)
addSubview(mainTitle)
subTitle.frame = CGRect(x: 0, y: 40, width: 300, height: 20)
subTitle.font = UIFont.systemFont(ofSize: 15)
addSubview(subTitle)
detail.frame = CGRect(x: 50, y: 60, width: 250, height: 80)
detail.font = UIFont.systemFont(ofSize: 10)
detail.numberOfLines = 0
addSubview(detail)
updateConfig()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateConfig(){
if let conf = configuration as? LargeImageConfig {
mainImageView.image = conf.mainImage
mainTitle.text = conf.mainTitle
subTitle.text = conf.subTitle
detail.text = conf.detail
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment