Skip to content

Commit db6b6c8

Browse files
committed
🚀 Add VerticalCollectionVC and HomeVC
1 parent d66dcf7 commit db6b6c8

File tree

8 files changed

+545
-1
lines changed

8 files changed

+545
-1
lines changed

SelfSizingCollectionViewDemo.xcodeproj/project.pbxproj

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
6657E4B423C0084200A15B02 /* HorizontalCollectionViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6657E4B223C0084200A15B02 /* HorizontalCollectionViewController.xib */; };
1515
6657E4B823C0096F00A15B02 /* HorizontalCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6657E4B623C0096F00A15B02 /* HorizontalCollectionViewCell.swift */; };
1616
6657E4B923C0096F00A15B02 /* HorizontalCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6657E4B723C0096F00A15B02 /* HorizontalCollectionViewCell.xib */; };
17+
66FFC83823C01C4100E864CA /* VerticalCollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66FFC83623C01C4100E864CA /* VerticalCollectionViewController.swift */; };
18+
66FFC83B23C01CD900E864CA /* VerticalCollectionViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 66FFC83A23C01CD900E864CA /* VerticalCollectionViewController.xib */; };
19+
66FFC83F23C01D1300E864CA /* VerticalCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 66FFC83D23C01D1300E864CA /* VerticalCollectionViewCell.xib */; };
20+
66FFC84023C01D1300E864CA /* VerticalCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66FFC83E23C01D1300E864CA /* VerticalCollectionViewCell.swift */; };
21+
66FFC84423C01D8500E864CA /* HomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66FFC84223C01D8500E864CA /* HomeViewController.swift */; };
22+
66FFC84523C01D8500E864CA /* HomeViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 66FFC84323C01D8500E864CA /* HomeViewController.xib */; };
1723
/* End PBXBuildFile section */
1824

1925
/* Begin PBXFileReference section */
@@ -26,6 +32,12 @@
2632
6657E4B223C0084200A15B02 /* HorizontalCollectionViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HorizontalCollectionViewController.xib; sourceTree = "<group>"; };
2733
6657E4B623C0096F00A15B02 /* HorizontalCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HorizontalCollectionViewCell.swift; sourceTree = "<group>"; };
2834
6657E4B723C0096F00A15B02 /* HorizontalCollectionViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HorizontalCollectionViewCell.xib; sourceTree = "<group>"; };
35+
66FFC83623C01C4100E864CA /* VerticalCollectionViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VerticalCollectionViewController.swift; sourceTree = "<group>"; };
36+
66FFC83A23C01CD900E864CA /* VerticalCollectionViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = VerticalCollectionViewController.xib; sourceTree = "<group>"; };
37+
66FFC83D23C01D1300E864CA /* VerticalCollectionViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = VerticalCollectionViewCell.xib; sourceTree = "<group>"; };
38+
66FFC83E23C01D1300E864CA /* VerticalCollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VerticalCollectionViewCell.swift; sourceTree = "<group>"; };
39+
66FFC84223C01D8500E864CA /* HomeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewController.swift; sourceTree = "<group>"; };
40+
66FFC84323C01D8500E864CA /* HomeViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HomeViewController.xib; sourceTree = "<group>"; };
2941
/* End PBXFileReference section */
3042

3143
/* Begin PBXFrameworksBuildPhase section */
@@ -71,7 +83,9 @@
7183
6657E4B023C0080400A15B02 /* UIViewController */ = {
7284
isa = PBXGroup;
7385
children = (
86+
66FFC84123C01D6900E864CA /* Home */,
7487
6657E4BA23C01A3100A15B02 /* Horizontal */,
88+
66FFC83523C01C4100E864CA /* Vertical */,
7589
);
7690
path = UIViewController;
7791
sourceTree = "<group>";
@@ -80,6 +94,7 @@
8094
isa = PBXGroup;
8195
children = (
8296
668E30EA23C01B200013C214 /* Horizontal */,
97+
66FFC83C23C01D1300E864CA /* Vertical */,
8398
);
8499
path = UICollectionViewCell;
85100
sourceTree = "<group>";
@@ -102,6 +117,33 @@
102117
path = Horizontal;
103118
sourceTree = "<group>";
104119
};
120+
66FFC83523C01C4100E864CA /* Vertical */ = {
121+
isa = PBXGroup;
122+
children = (
123+
66FFC83623C01C4100E864CA /* VerticalCollectionViewController.swift */,
124+
66FFC83A23C01CD900E864CA /* VerticalCollectionViewController.xib */,
125+
);
126+
path = Vertical;
127+
sourceTree = "<group>";
128+
};
129+
66FFC83C23C01D1300E864CA /* Vertical */ = {
130+
isa = PBXGroup;
131+
children = (
132+
66FFC83E23C01D1300E864CA /* VerticalCollectionViewCell.swift */,
133+
66FFC83D23C01D1300E864CA /* VerticalCollectionViewCell.xib */,
134+
);
135+
path = Vertical;
136+
sourceTree = "<group>";
137+
};
138+
66FFC84123C01D6900E864CA /* Home */ = {
139+
isa = PBXGroup;
140+
children = (
141+
66FFC84223C01D8500E864CA /* HomeViewController.swift */,
142+
66FFC84323C01D8500E864CA /* HomeViewController.xib */,
143+
);
144+
path = Home;
145+
sourceTree = "<group>";
146+
};
105147
/* End PBXGroup section */
106148

107149
/* Begin PBXNativeTarget section */
@@ -160,9 +202,12 @@
160202
isa = PBXResourcesBuildPhase;
161203
buildActionMask = 2147483647;
162204
files = (
205+
66FFC83B23C01CD900E864CA /* VerticalCollectionViewController.xib in Resources */,
163206
6657E4B923C0096F00A15B02 /* HorizontalCollectionViewCell.xib in Resources */,
207+
66FFC83F23C01D1300E864CA /* VerticalCollectionViewCell.xib in Resources */,
164208
6657E4B423C0084200A15B02 /* HorizontalCollectionViewController.xib in Resources */,
165209
6657E4A923C007B000A15B02 /* LaunchScreen.storyboard in Resources */,
210+
66FFC84523C01D8500E864CA /* HomeViewController.xib in Resources */,
166211
6657E4A623C007B000A15B02 /* Assets.xcassets in Resources */,
167212
);
168213
runOnlyForDeploymentPostprocessing = 0;
@@ -174,9 +219,12 @@
174219
isa = PBXSourcesBuildPhase;
175220
buildActionMask = 2147483647;
176221
files = (
222+
66FFC84023C01D1300E864CA /* VerticalCollectionViewCell.swift in Sources */,
177223
6657E4B323C0084200A15B02 /* HorizontalCollectionViewController.swift in Sources */,
178224
6657E4B823C0096F00A15B02 /* HorizontalCollectionViewCell.swift in Sources */,
225+
66FFC83823C01C4100E864CA /* VerticalCollectionViewController.swift in Sources */,
179226
6657E49D23C007AC00A15B02 /* AppDelegate.swift in Sources */,
227+
66FFC84423C01D8500E864CA /* HomeViewController.swift in Sources */,
180228
);
181229
runOnlyForDeploymentPostprocessing = 0;
182230
};

SelfSizingCollectionViewDemo/AppDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1515

1616
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1717
let window = UIWindow()
18-
window.rootViewController = HorizontalCollectionViewController()
18+
let viewController = HomeViewController()
19+
window.rootViewController = UINavigationController(rootViewController: viewController)
1920
window.makeKeyAndVisible()
2021
self.window = window
2122
return true
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// VerticalCollectionViewCell.swift
3+
// SelfSizingCollectionViewDemo
4+
//
5+
// Created by Reona Kubo on 2020/01/04.
6+
// Copyright © 2020 Reona Kubo. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
final class VerticalCollectionViewCell: UICollectionViewCell {
12+
13+
@IBOutlet private weak var textLabel: UILabel!
14+
15+
override func awakeFromNib() {
16+
super.awakeFromNib()
17+
activationSelfSizing()
18+
}
19+
20+
func configure(text: String) {
21+
textLabel.text = text
22+
}
23+
24+
private func activationSelfSizing() {
25+
// iOS12 のときに Self-sizing が有効にならない対策
26+
contentView.translatesAutoresizingMaskIntoConstraints = false
27+
let leftConstraint = contentView.leftAnchor.constraint(equalTo: leftAnchor)
28+
let rightConstraint = contentView.rightAnchor.constraint(equalTo: rightAnchor)
29+
let topConstraint = contentView.topAnchor.constraint(equalTo: topAnchor)
30+
let bottomConstraint = contentView.bottomAnchor.constraint(equalTo: bottomAnchor)
31+
NSLayoutConstraint.activate([leftConstraint, rightConstraint, topConstraint, bottomConstraint])
32+
}
33+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="15702" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
3+
<device id="retina6_1" orientation="portrait" appearance="light"/>
4+
<dependencies>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15704"/>
6+
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
7+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
8+
</dependencies>
9+
<objects>
10+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
11+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
12+
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="gTV-IL-0wX" customClass="VerticalCollectionViewCell" customModule="SelfSizingCollectionViewDemo" customModuleProvider="target">
13+
<rect key="frame" x="0.0" y="0.0" width="245" height="50"/>
14+
<autoresizingMask key="autoresizingMask"/>
15+
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
16+
<rect key="frame" x="0.0" y="0.0" width="245" height="50"/>
17+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
18+
<subviews>
19+
<view clipsSubviews="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="auu-Rj-XYc">
20+
<rect key="frame" x="0.0" y="0.0" width="245" height="50"/>
21+
<subviews>
22+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dzG-Bo-cbG">
23+
<rect key="frame" x="16" y="8" width="213" height="34"/>
24+
<fontDescription key="fontDescription" type="boldSystem" pointSize="14"/>
25+
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
26+
<nil key="highlightedColor"/>
27+
</label>
28+
</subviews>
29+
<color key="backgroundColor" systemColor="systemIndigoColor" red="0.34509803919999998" green="0.33725490200000002" blue="0.83921568629999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
30+
<constraints>
31+
<constraint firstItem="dzG-Bo-cbG" firstAttribute="leading" secondItem="auu-Rj-XYc" secondAttribute="leading" constant="16" id="DBG-kU-Kjo"/>
32+
<constraint firstAttribute="trailing" secondItem="dzG-Bo-cbG" secondAttribute="trailing" constant="16" id="YYF-sb-FkJ"/>
33+
<constraint firstAttribute="bottom" secondItem="dzG-Bo-cbG" secondAttribute="bottom" constant="8" id="caA-4m-bHj"/>
34+
<constraint firstItem="dzG-Bo-cbG" firstAttribute="top" secondItem="auu-Rj-XYc" secondAttribute="top" constant="8" id="yCo-Bv-gtX"/>
35+
</constraints>
36+
<userDefinedRuntimeAttributes>
37+
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
38+
<integer key="value" value="15"/>
39+
</userDefinedRuntimeAttribute>
40+
</userDefinedRuntimeAttributes>
41+
</view>
42+
</subviews>
43+
</view>
44+
<constraints>
45+
<constraint firstItem="auu-Rj-XYc" firstAttribute="leading" secondItem="gTV-IL-0wX" secondAttribute="leading" id="ARu-s4-8Eh"/>
46+
<constraint firstItem="auu-Rj-XYc" firstAttribute="top" secondItem="gTV-IL-0wX" secondAttribute="top" id="Bfl-fb-81Y"/>
47+
<constraint firstAttribute="trailing" secondItem="auu-Rj-XYc" secondAttribute="trailing" id="mKo-7X-RNV"/>
48+
<constraint firstAttribute="bottom" secondItem="auu-Rj-XYc" secondAttribute="bottom" id="qHH-MB-sAD"/>
49+
</constraints>
50+
<viewLayoutGuide key="safeArea" id="ZTg-uK-7eu"/>
51+
<size key="customSize" width="245" height="50"/>
52+
<connections>
53+
<outlet property="textLabel" destination="dzG-Bo-cbG" id="4Wb-Qq-Dg6"/>
54+
</connections>
55+
<point key="canvasLocation" x="273.1884057971015" y="126.5625"/>
56+
</collectionViewCell>
57+
</objects>
58+
</document>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// HomeViewController.swift
3+
// SelfSizingCollectionViewDemo
4+
//
5+
// Created by Reona Kubo on 2020/01/04.
6+
// Copyright © 2020 Reona Kubo. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
final class HomeViewController: UIViewController {
12+
13+
override func viewDidLoad() {
14+
super.viewDidLoad()
15+
}
16+
17+
@IBAction func showHorizontalViewController(_ sender: UIButton) {
18+
let viewController = HorizontalCollectionViewController()
19+
navigationController?.pushViewController(viewController, animated: true)
20+
}
21+
22+
@IBAction func showVerticalViewController(_ sender: UIButton) {
23+
let viewController = VerticalCollectionViewController()
24+
navigationController?.pushViewController(viewController, animated: true)
25+
}
26+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="15702" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
3+
<device id="retina6_1" orientation="portrait" appearance="light"/>
4+
<dependencies>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15704"/>
6+
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
7+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
8+
</dependencies>
9+
<objects>
10+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="HomeViewController" customModule="SelfSizingCollectionViewDemo" customModuleProvider="target">
11+
<connections>
12+
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
13+
</connections>
14+
</placeholder>
15+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
16+
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
17+
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
18+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
19+
<subviews>
20+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="80r-Zg-Hdt">
21+
<rect key="frame" x="171.5" y="294" width="71" height="30"/>
22+
<state key="normal" title="Horizontal"/>
23+
<connections>
24+
<action selector="showHorizontalViewController:" destination="-1" eventType="touchUpInside" id="OGX-Sd-hpC"/>
25+
</connections>
26+
</button>
27+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="UBy-HC-MyV">
28+
<rect key="frame" x="181" y="372" width="52" height="30"/>
29+
<state key="normal" title="Vertical"/>
30+
<connections>
31+
<action selector="showVerticalViewController:" destination="-1" eventType="touchUpInside" id="ivC-eM-itc"/>
32+
</connections>
33+
</button>
34+
</subviews>
35+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
36+
<constraints>
37+
<constraint firstItem="80r-Zg-Hdt" firstAttribute="top" secondItem="fnl-2z-Ty3" secondAttribute="top" constant="250" id="BmO-qE-f5g"/>
38+
<constraint firstItem="UBy-HC-MyV" firstAttribute="top" secondItem="80r-Zg-Hdt" secondAttribute="bottom" constant="48" id="D6L-Vg-xwT"/>
39+
<constraint firstItem="UBy-HC-MyV" firstAttribute="centerX" secondItem="i5M-Pr-FkT" secondAttribute="centerX" id="qq9-tR-nPC"/>
40+
<constraint firstItem="80r-Zg-Hdt" firstAttribute="centerX" secondItem="i5M-Pr-FkT" secondAttribute="centerX" id="zUp-RN-yWX"/>
41+
</constraints>
42+
<viewLayoutGuide key="safeArea" id="fnl-2z-Ty3"/>
43+
<point key="canvasLocation" x="132" y="127"/>
44+
</view>
45+
</objects>
46+
</document>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// VerticalCollectionViewController.swift
3+
// SelfSizingCollectionViewDemo
4+
//
5+
// Created by Reona Kubo on 2020/01/04.
6+
// Copyright © 2020 Reona Kubo. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
final class VerticalCollectionViewController: UIViewController {
12+
13+
@IBOutlet private var collectionViews: [UICollectionView]!
14+
15+
private let reuseIdentifier = "cell"
16+
private let animals: [String] = ["cat", "kangaroo", "seal", "dog", "panda", "giraffe", "penguin", "zebra", "hippopotamus"]
17+
18+
override func viewDidLoad() {
19+
super.viewDidLoad()
20+
for collectionView in collectionViews {
21+
collectionView.delegate = self
22+
collectionView.dataSource = self
23+
collectionView.register(UINib(nibName: "VerticalCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
24+
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
25+
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
26+
layout.invalidateLayout()
27+
}
28+
}
29+
}
30+
}
31+
32+
// MARK: - UICollectionViewDataSource
33+
34+
extension VerticalCollectionViewController: UICollectionViewDataSource {
35+
36+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
37+
return animals.count
38+
}
39+
40+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
41+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! VerticalCollectionViewCell
42+
cell.configure(text: animals[indexPath.row])
43+
return cell
44+
}
45+
46+
}
47+
48+
// MARK: - UICollectionViewDelegate
49+
50+
extension VerticalCollectionViewController: UICollectionViewDelegate {}

0 commit comments

Comments
 (0)