Skip to content

Commit d88381b

Browse files
committed
🚀 Add Self-sizing CollectionViewController
1 parent 1ffc08a commit d88381b

File tree

10 files changed

+236
-156
lines changed

10 files changed

+236
-156
lines changed

SelfSizingCollectionViewDemo.xcodeproj/project.pbxproj

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@
88

99
/* Begin PBXBuildFile section */
1010
6657E49D23C007AC00A15B02 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6657E49C23C007AC00A15B02 /* AppDelegate.swift */; };
11-
6657E49F23C007AC00A15B02 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6657E49E23C007AC00A15B02 /* SceneDelegate.swift */; };
12-
6657E4A123C007AC00A15B02 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6657E4A023C007AC00A15B02 /* ViewController.swift */; };
13-
6657E4A423C007AC00A15B02 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6657E4A223C007AC00A15B02 /* Main.storyboard */; };
1411
6657E4A623C007B000A15B02 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6657E4A523C007B000A15B02 /* Assets.xcassets */; };
1512
6657E4A923C007B000A15B02 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6657E4A723C007B000A15B02 /* LaunchScreen.storyboard */; };
13+
6657E4B323C0084200A15B02 /* CollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6657E4B123C0084200A15B02 /* CollectionViewController.swift */; };
14+
6657E4B423C0084200A15B02 /* CollectionViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6657E4B223C0084200A15B02 /* CollectionViewController.xib */; };
15+
6657E4B823C0096F00A15B02 /* SelfSizingCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6657E4B623C0096F00A15B02 /* SelfSizingCollectionViewCell.swift */; };
16+
6657E4B923C0096F00A15B02 /* SelfSizingCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6657E4B723C0096F00A15B02 /* SelfSizingCollectionViewCell.xib */; };
1617
/* End PBXBuildFile section */
1718

1819
/* Begin PBXFileReference section */
1920
6657E49923C007AC00A15B02 /* SelfSizingCollectionViewDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SelfSizingCollectionViewDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
2021
6657E49C23C007AC00A15B02 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
21-
6657E49E23C007AC00A15B02 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
22-
6657E4A023C007AC00A15B02 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
23-
6657E4A323C007AC00A15B02 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
2422
6657E4A523C007B000A15B02 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2523
6657E4A823C007B000A15B02 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2624
6657E4AA23C007B000A15B02 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
25+
6657E4B123C0084200A15B02 /* CollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewController.swift; sourceTree = "<group>"; };
26+
6657E4B223C0084200A15B02 /* CollectionViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CollectionViewController.xib; sourceTree = "<group>"; };
27+
6657E4B623C0096F00A15B02 /* SelfSizingCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelfSizingCollectionViewCell.swift; sourceTree = "<group>"; };
28+
6657E4B723C0096F00A15B02 /* SelfSizingCollectionViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SelfSizingCollectionViewCell.xib; sourceTree = "<group>"; };
2729
/* End PBXFileReference section */
2830

2931
/* Begin PBXFrameworksBuildPhase section */
@@ -57,16 +59,33 @@
5759
isa = PBXGroup;
5860
children = (
5961
6657E49C23C007AC00A15B02 /* AppDelegate.swift */,
60-
6657E49E23C007AC00A15B02 /* SceneDelegate.swift */,
61-
6657E4A023C007AC00A15B02 /* ViewController.swift */,
62-
6657E4A223C007AC00A15B02 /* Main.storyboard */,
62+
6657E4B523C0094A00A15B02 /* UICollectionViewCell */,
63+
6657E4B023C0080400A15B02 /* UIViewController */,
6364
6657E4A523C007B000A15B02 /* Assets.xcassets */,
6465
6657E4A723C007B000A15B02 /* LaunchScreen.storyboard */,
6566
6657E4AA23C007B000A15B02 /* Info.plist */,
6667
);
6768
path = SelfSizingCollectionViewDemo;
6869
sourceTree = "<group>";
6970
};
71+
6657E4B023C0080400A15B02 /* UIViewController */ = {
72+
isa = PBXGroup;
73+
children = (
74+
6657E4B123C0084200A15B02 /* CollectionViewController.swift */,
75+
6657E4B223C0084200A15B02 /* CollectionViewController.xib */,
76+
);
77+
path = UIViewController;
78+
sourceTree = "<group>";
79+
};
80+
6657E4B523C0094A00A15B02 /* UICollectionViewCell */ = {
81+
isa = PBXGroup;
82+
children = (
83+
6657E4B623C0096F00A15B02 /* SelfSizingCollectionViewCell.swift */,
84+
6657E4B723C0096F00A15B02 /* SelfSizingCollectionViewCell.xib */,
85+
);
86+
path = UICollectionViewCell;
87+
sourceTree = "<group>";
88+
};
7089
/* End PBXGroup section */
7190

7291
/* Begin PBXNativeTarget section */
@@ -125,9 +144,10 @@
125144
isa = PBXResourcesBuildPhase;
126145
buildActionMask = 2147483647;
127146
files = (
147+
6657E4B923C0096F00A15B02 /* SelfSizingCollectionViewCell.xib in Resources */,
148+
6657E4B423C0084200A15B02 /* CollectionViewController.xib in Resources */,
128149
6657E4A923C007B000A15B02 /* LaunchScreen.storyboard in Resources */,
129150
6657E4A623C007B000A15B02 /* Assets.xcassets in Resources */,
130-
6657E4A423C007AC00A15B02 /* Main.storyboard in Resources */,
131151
);
132152
runOnlyForDeploymentPostprocessing = 0;
133153
};
@@ -138,23 +158,15 @@
138158
isa = PBXSourcesBuildPhase;
139159
buildActionMask = 2147483647;
140160
files = (
141-
6657E4A123C007AC00A15B02 /* ViewController.swift in Sources */,
161+
6657E4B323C0084200A15B02 /* CollectionViewController.swift in Sources */,
162+
6657E4B823C0096F00A15B02 /* SelfSizingCollectionViewCell.swift in Sources */,
142163
6657E49D23C007AC00A15B02 /* AppDelegate.swift in Sources */,
143-
6657E49F23C007AC00A15B02 /* SceneDelegate.swift in Sources */,
144164
);
145165
runOnlyForDeploymentPostprocessing = 0;
146166
};
147167
/* End PBXSourcesBuildPhase section */
148168

149169
/* Begin PBXVariantGroup section */
150-
6657E4A223C007AC00A15B02 /* Main.storyboard */ = {
151-
isa = PBXVariantGroup;
152-
children = (
153-
6657E4A323C007AC00A15B02 /* Base */,
154-
);
155-
name = Main.storyboard;
156-
sourceTree = "<group>";
157-
};
158170
6657E4A723C007B000A15B02 /* LaunchScreen.storyboard */ = {
159171
isa = PBXVariantGroup;
160172
children = (

SelfSizingCollectionViewDemo/AppDelegate.swift

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,14 @@ import UIKit
1111
@UIApplicationMain
1212
class AppDelegate: UIResponder, UIApplicationDelegate {
1313

14-
14+
var window: UIWindow?
1515

1616
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
17-
// Override point for customization after application launch.
17+
let window = UIWindow()
18+
window.rootViewController = CollectionViewController()
19+
window.makeKeyAndVisible()
20+
self.window = window
1821
return true
1922
}
20-
21-
// MARK: UISceneSession Lifecycle
22-
23-
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
24-
// Called when a new scene session is being created.
25-
// Use this method to select a configuration to create the new scene with.
26-
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
27-
}
28-
29-
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
30-
// Called when the user discards a scene session.
31-
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
32-
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
33-
}
34-
35-
3623
}
3724

SelfSizingCollectionViewDemo/Base.lproj/Main.storyboard

Lines changed: 0 additions & 24 deletions
This file was deleted.

SelfSizingCollectionViewDemo/Info.plist

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,8 @@
2020
<string>1</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
23-
<key>UIApplicationSceneManifest</key>
24-
<dict>
25-
<key>UIApplicationSupportsMultipleScenes</key>
26-
<false/>
27-
<key>UISceneConfigurations</key>
28-
<dict>
29-
<key>UIWindowSceneSessionRoleApplication</key>
30-
<array>
31-
<dict>
32-
<key>UISceneConfigurationName</key>
33-
<string>Default Configuration</string>
34-
<key>UISceneDelegateClassName</key>
35-
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
36-
<key>UISceneStoryboardFile</key>
37-
<string>Main</string>
38-
</dict>
39-
</array>
40-
</dict>
41-
</dict>
4223
<key>UILaunchStoryboardName</key>
4324
<string>LaunchScreen</string>
44-
<key>UIMainStoryboardFile</key>
45-
<string>Main</string>
4625
<key>UIRequiredDeviceCapabilities</key>
4726
<array>
4827
<string>armv7</string>

SelfSizingCollectionViewDemo/SceneDelegate.swift

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// SelfSizingCollectionViewCell.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 SelfSizingCollectionViewCell: 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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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="Named colors" minToolsVersion="9.0"/>
7+
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
8+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
9+
</dependencies>
10+
<customFonts key="customFonts">
11+
<array key="NotoSansCJKjp-Bold.otf">
12+
<string>NotoSansCJKjp-Bold</string>
13+
</array>
14+
</customFonts>
15+
<objects>
16+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
17+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
18+
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="gTV-IL-0wX" customClass="SelfSizingCollectionViewCell" customModule="SelfSizingCollectionViewDemo" customModuleProvider="target">
19+
<rect key="frame" x="0.0" y="0.0" width="245" height="50"/>
20+
<autoresizingMask key="autoresizingMask"/>
21+
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
22+
<rect key="frame" x="0.0" y="0.0" width="245" height="50"/>
23+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
24+
<subviews>
25+
<view clipsSubviews="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="auu-Rj-XYc">
26+
<rect key="frame" x="0.0" y="0.0" width="245" height="50"/>
27+
<subviews>
28+
<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">
29+
<rect key="frame" x="16" y="8" width="213" height="34"/>
30+
<fontDescription key="fontDescription" name="NotoSansCJKjp-Bold" family="Noto Sans CJK JP" pointSize="14"/>
31+
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
32+
<nil key="highlightedColor"/>
33+
</label>
34+
</subviews>
35+
<color key="backgroundColor" name="placeholderColor"/>
36+
<constraints>
37+
<constraint firstItem="dzG-Bo-cbG" firstAttribute="leading" secondItem="auu-Rj-XYc" secondAttribute="leading" constant="16" id="DBG-kU-Kjo"/>
38+
<constraint firstAttribute="trailing" secondItem="dzG-Bo-cbG" secondAttribute="trailing" constant="16" id="YYF-sb-FkJ"/>
39+
<constraint firstAttribute="bottom" secondItem="dzG-Bo-cbG" secondAttribute="bottom" constant="8" id="caA-4m-bHj"/>
40+
<constraint firstItem="dzG-Bo-cbG" firstAttribute="top" secondItem="auu-Rj-XYc" secondAttribute="top" constant="8" id="yCo-Bv-gtX"/>
41+
</constraints>
42+
<userDefinedRuntimeAttributes>
43+
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
44+
<integer key="value" value="15"/>
45+
</userDefinedRuntimeAttribute>
46+
</userDefinedRuntimeAttributes>
47+
</view>
48+
</subviews>
49+
</view>
50+
<constraints>
51+
<constraint firstItem="auu-Rj-XYc" firstAttribute="leading" secondItem="gTV-IL-0wX" secondAttribute="leading" id="ARu-s4-8Eh"/>
52+
<constraint firstItem="auu-Rj-XYc" firstAttribute="top" secondItem="gTV-IL-0wX" secondAttribute="top" id="Bfl-fb-81Y"/>
53+
<constraint firstAttribute="trailing" secondItem="auu-Rj-XYc" secondAttribute="trailing" id="mKo-7X-RNV"/>
54+
<constraint firstAttribute="bottom" secondItem="auu-Rj-XYc" secondAttribute="bottom" id="qHH-MB-sAD"/>
55+
</constraints>
56+
<viewLayoutGuide key="safeArea" id="ZTg-uK-7eu"/>
57+
<size key="customSize" width="245" height="50"/>
58+
<connections>
59+
<outlet property="textLabel" destination="dzG-Bo-cbG" id="4Wb-Qq-Dg6"/>
60+
</connections>
61+
<point key="canvasLocation" x="273.1884057971015" y="126.5625"/>
62+
</collectionViewCell>
63+
</objects>
64+
<resources>
65+
<namedColor name="placeholderColor">
66+
<color red="0.32899999618530273" green="0.32400000095367432" blue="0.4779999852180481" alpha="0.33000001311302185" colorSpace="custom" customColorSpace="sRGB"/>
67+
</namedColor>
68+
</resources>
69+
</document>

0 commit comments

Comments
 (0)