From f19a09c0e91668a48c1958fc9a4fa07f963c4142 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Sat, 19 Mar 2022 13:08:40 +1100 Subject: Detail view is now programatically created Using SnapKit and some experimentation around the VC. Next step is to change segue and then maybe get rid of storyboard all together. --- foray/Base.lproj/Main.storyboard | 51 +-------------------- foray/ForayDetailViewController.swift | 28 ++++-------- foray/ForayNewDetailView.swift | 85 +++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 70 deletions(-) create mode 100644 foray/ForayNewDetailView.swift (limited to 'foray') diff --git a/foray/Base.lproj/Main.storyboard b/foray/Base.lproj/Main.storyboard index a655965..6e8fa72 100644 --- a/foray/Base.lproj/Main.storyboard +++ b/foray/Base.lproj/Main.storyboard @@ -3,8 +3,6 @@ - - @@ -48,49 +46,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -99,7 +55,7 @@ - + @@ -115,9 +71,4 @@ - - - - - diff --git a/foray/ForayDetailViewController.swift b/foray/ForayDetailViewController.swift index eb5b577..bac2dfd 100644 --- a/foray/ForayDetailViewController.swift +++ b/foray/ForayDetailViewController.swift @@ -10,31 +10,19 @@ import UIKit class ForayDetailViewController: UIViewController { var selectedItem: PenguinItem! - - @IBOutlet weak var titleLabel: UILabel! - @IBOutlet weak var imageView: UIImageView! - @IBOutlet weak var detailLabel: UILabel! + + let detailView: ForayNewDetailView = ForayNewDetailView(frame: CGRect.zero) override func viewDidLoad() { super.viewDidLoad() - - titleLabel.text = selectedItem.name - imageView.image = UIImage(named: selectedItem.id) - detailLabel.text = "Type: " - switch selectedItem.type { - case .item: - detailLabel.text! += "Item" - case .quest: - detailLabel.text! += "Quest" - imageView.image = UIImage(named: "spy") - } - - detailLabel.text! += "\nID: " + selectedItem.id + self.view.backgroundColor = .systemBackground - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "yyyy-MM-dd" - detailLabel.text! += "\nReleased: " + dateFormatter.string(from: selectedItem.releaseDate) + detailView.setSelectedItem(selectedItem: selectedItem) + self.view.addSubview(detailView) + detailView.snp.makeConstraints { (make) in + make.edges.equalTo(self.view.snp.margins) + } } } diff --git a/foray/ForayNewDetailView.swift b/foray/ForayNewDetailView.swift new file mode 100644 index 0000000..e7d7dbf --- /dev/null +++ b/foray/ForayNewDetailView.swift @@ -0,0 +1,85 @@ +// +// ForayNewDetailView.swift +// foray +// +// Created by Nicholas Tay on 18/3/2022. +// + +import UIKit + +class ForayNewDetailView: UIView { + + let nameLabel: UILabel = { + let l = UILabel() + l.font = UIFont.preferredFont(forTextStyle: .largeTitle) + l.adjustsFontForContentSizeCategory = true + l.numberOfLines = 3 + l.textAlignment = .center + return l + }() + + let itemImageView: UIImageView = { + let iv = UIImageView() + iv.contentMode = .scaleAspectFit + return iv + }() + + let descLabel: UILabel = { + let l = UILabel() + l.font = UIFont.preferredFont(forTextStyle: .body) + l.adjustsFontForContentSizeCategory = true + return l + }() + + override init(frame: CGRect) { + super.init(frame: frame) + initialiseViews() + } + + required init?(coder: NSCoder) { + fatalError("unreachable") + } + + private func initialiseViews() { + addSubview(nameLabel) + addSubview(itemImageView) + addSubview(descLabel) + setupConstraints() + } + + private func setupConstraints() { + nameLabel.snp.makeConstraints { (make) in + make.top.equalToSuperview().inset(8) + make.leading.trailing.equalToSuperview().inset(8) + } + itemImageView.snp.makeConstraints { (make) in + make.top.equalTo(nameLabel.snp.bottom).offset(8) + make.leading.trailing.equalToSuperview() + make.height.equalTo(150) + } + descLabel.snp.makeConstraints { (make) in + make.top.equalTo(itemImageView.snp.bottom).offset(8) + make.leading.trailing.equalToSuperview() + } + } + + public func setSelectedItem(selectedItem: PenguinItem) { + nameLabel.text = selectedItem.name + itemImageView.image = UIImage(named: selectedItem.id) + + descLabel.text = "Type: " + switch selectedItem.type { + case .item: + descLabel.text! += "Item" + case .quest: + descLabel.text! += "Quest" + itemImageView.image = UIImage(named: "spy") + } + + descLabel.text! += "\nID: " + selectedItem.id + + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd" + descLabel.text! += "\nReleased: " + dateFormatter.string(from: selectedItem.releaseDate) + } +} -- cgit