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. --- .gitignore | 1 + foray.xcodeproj/project.pbxproj | 4 ++ foray/Base.lproj/Main.storyboard | 51 +-------------------- foray/ForayDetailViewController.swift | 28 ++++-------- foray/ForayNewDetailView.swift | 85 +++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 70 deletions(-) create mode 100644 foray/ForayNewDetailView.swift diff --git a/.gitignore b/.gitignore index 3370af8..72c6f84 100644 --- a/.gitignore +++ b/.gitignore @@ -91,3 +91,4 @@ iOSInjectionProject/ .DS_Store +*.swp diff --git a/foray.xcodeproj/project.pbxproj b/foray.xcodeproj/project.pbxproj index 50615ca..3423ae5 100644 --- a/foray.xcodeproj/project.pbxproj +++ b/foray.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ C04B45B827DEF2ED001451A3 /* ForayTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04B45B727DEF2ED001451A3 /* ForayTableViewController.swift */; }; C04EDE4227E428AB00D83005 /* SnapKit in Frameworks */ = {isa = PBXBuildFile; productRef = C04EDE4127E428AB00D83005 /* SnapKit */; }; C04EDE4427E4298D00D83005 /* ForayNewTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04EDE4327E4298D00D83005 /* ForayNewTableViewCell.swift */; }; + C04EDE4627E45CCA00D83005 /* ForayNewDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04EDE4527E45CCA00D83005 /* ForayNewDetailView.swift */; }; C0FEAF5F27E14C52000A7648 /* ForayDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0FEAF5E27E14C52000A7648 /* ForayDetailViewController.swift */; }; /* End PBXBuildFile section */ @@ -28,6 +29,7 @@ C04B45B127DEF118001451A3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; C04B45B727DEF2ED001451A3 /* ForayTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayTableViewController.swift; sourceTree = ""; }; C04EDE4327E4298D00D83005 /* ForayNewTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayNewTableViewCell.swift; sourceTree = ""; }; + C04EDE4527E45CCA00D83005 /* ForayNewDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayNewDetailView.swift; sourceTree = ""; }; C0FEAF5E27E14C52000A7648 /* ForayDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayDetailViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -65,6 +67,7 @@ C04B45A327DEF117001451A3 /* AppDelegate.swift */, C04B45A527DEF117001451A3 /* SceneDelegate.swift */, C04B45A927DEF117001451A3 /* Main.storyboard */, + C04EDE4527E45CCA00D83005 /* ForayNewDetailView.swift */, C0FEAF5E27E14C52000A7648 /* ForayDetailViewController.swift */, C04B45B727DEF2ED001451A3 /* ForayTableViewController.swift */, C04EDE4327E4298D00D83005 /* ForayNewTableViewCell.swift */, @@ -154,6 +157,7 @@ files = ( C04B45B827DEF2ED001451A3 /* ForayTableViewController.swift in Sources */, C0FEAF5F27E14C52000A7648 /* ForayDetailViewController.swift in Sources */, + C04EDE4627E45CCA00D83005 /* ForayNewDetailView.swift in Sources */, C04EDE4427E4298D00D83005 /* ForayNewTableViewCell.swift in Sources */, C04B45A427DEF117001451A3 /* AppDelegate.swift in Sources */, C04B45A627DEF117001451A3 /* SceneDelegate.swift in Sources */, 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