Skip to content

Commit 6d834cd

Browse files
committed
Adds convenience sizing methods for views that use AutoLayout and are capable of calculating their sizes.
1 parent 124f142 commit 6d834cd

File tree

6 files changed

+71
-13
lines changed

6 files changed

+71
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.
33

44
# Next
55

6+
* Added convenience methods, that allow to resize view properly if view is using AutoLayout and can determine it's desired size: `compressedLayout()`, `expandedLayout()` and `systemLayout(fittingSize:, horizontalPriority:, verticalPriority:)`.
7+
* Dropped support for Swift 3.
8+
69
## [3.2.0](https://github.com/MLSDev/LoadableViews/releases/tag/3.2.0)
710

811
* Support for Swift 5 and Xcode 10.2.

Gemfile.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ GEM
1212
atomos (0.1.3)
1313
babosa (1.0.2)
1414
claide (1.0.2)
15-
cocoapods (1.7.0.beta.2)
15+
cocoapods (1.7.0.beta.3)
1616
activesupport (>= 4.0.2, < 5)
1717
claide (>= 1.0.2, < 2.0)
18-
cocoapods-core (= 1.7.0.beta.2)
18+
cocoapods-core (= 1.7.0.beta.3)
1919
cocoapods-deintegrate (>= 1.0.3, < 2.0)
2020
cocoapods-downloader (>= 1.2.2, < 2.0)
2121
cocoapods-plugins (>= 1.0.0, < 2.0)
@@ -30,12 +30,12 @@ GEM
3030
molinillo (~> 0.6.6)
3131
nap (~> 1.0)
3232
ruby-macho (~> 1.4)
33-
xcodeproj (>= 1.8.1, < 2.0)
34-
cocoapods-core (1.7.0.beta.2)
33+
xcodeproj (>= 1.8.2, < 2.0)
34+
cocoapods-core (1.7.0.beta.3)
3535
activesupport (>= 4.0.2, < 6)
3636
fuzzy_match (~> 2.0.4)
3737
nap (~> 1.0)
38-
cocoapods-deintegrate (1.0.3)
38+
cocoapods-deintegrate (1.0.4)
3939
cocoapods-downloader (1.2.2)
4040
cocoapods-plugins (1.0.0)
4141
nap
@@ -142,7 +142,7 @@ GEM
142142
memoist (0.16.0)
143143
mime-types (3.2.2)
144144
mime-types-data (~> 3.2015)
145-
mime-types-data (3.2018.0812)
145+
mime-types-data (3.2019.0331)
146146
mini_magick (4.5.1)
147147
minitest (5.11.3)
148148
molinillo (0.6.6)
@@ -153,7 +153,7 @@ GEM
153153
nap (1.1.0)
154154
naturally (2.2.0)
155155
netrc (0.11.0)
156-
octokit (4.13.0)
156+
octokit (4.14.0)
157157
sawyer (~> 0.8.0, >= 0.5.3)
158158
os (1.0.0)
159159
plist (3.5.0)
@@ -195,7 +195,7 @@ GEM
195195
unf_ext (0.0.7.5)
196196
unicode-display_width (1.5.0)
197197
word_wrap (1.0.0)
198-
xcodeproj (1.8.1)
198+
xcodeproj (1.8.2)
199199
CFPropertyList (>= 2.3.3, < 4.0)
200200
atomos (~> 0.1.3)
201201
claide (>= 1.0.2, < 2.0)

LoadableViews.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Pod::Spec.new do |s|
99
s.social_media_url = "https://twitter.com/mlsdevcom"
1010
s.ios.deployment_target = "8.0"
1111
s.tvos.deployment_target = "9.0"
12-
s.swift_versions = ['3.2', '4.0', '4.2', '5.0']
12+
s.swift_versions = ['4.0', '4.2', '5.0']
1313
s.source = { git: "https://github.com/MLSDev/LoadableViews.git", tag: s.version.to_s }
1414
s.source_files = "Source/*.swift"
1515
s.framework = "UIKit"

Source/LoadableView.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,41 @@ extension NibLoadableProtocol {
7373
}
7474
}
7575

76+
extension NibLoadableProtocol where Self: UIView {
77+
78+
/// Sets the frame of the view to result of `systemLayoutSizeFitting` method call with `UIView.layoutFittingCompressedSize` parameter.
79+
///
80+
/// - Returns: loadable view
81+
public func compressedLayout() -> Self {
82+
frame.size = systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
83+
return self
84+
}
85+
86+
/// Sets the frame of the view to result of `systemLayoutSizeFitting` method call with `UIView.layoutFittingExpandedSize` parameter.
87+
///
88+
/// - Returns: loadable view
89+
public func expandedLayout() -> Self {
90+
frame.size = systemLayoutSizeFitting(UIView.layoutFittingExpandedSize)
91+
return self
92+
}
93+
94+
/// Sets the frame of the view to result of `systemLayoutSizeFitting` method call with provided parameters.
95+
///
96+
/// - Parameters:
97+
/// - fittingSize: fittingSize to be passed to `systemLayoutSizeFitting` method.
98+
/// - horizontalPriority: horizontal priority to be passed to `systemLayoutSizeFitting` method.
99+
/// - verticalPriority: vertical priority to be passed to `systemLayoutSizeFitting` method.
100+
/// - Returns: loadable view
101+
public func systemLayout(fittingSize: CGSize,
102+
horizontalPriority: UILayoutPriority,
103+
verticalPriority: UILayoutPriority) -> Self {
104+
frame.size = systemLayoutSizeFitting(fittingSize,
105+
withHorizontalFittingPriority: horizontalPriority,
106+
verticalFittingPriority: verticalPriority)
107+
return self
108+
}
109+
}
110+
76111
/// UIView subclass, that can be loaded into different xib or storyboard by simply referencing it's class.
77112
open class LoadableView: UIView, NibLoadableProtocol {
78113

TestFixtures/iOSTestableView.xib

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1010
</dependencies>
1111
<objects>
12-
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="iOSTestableView" customModule="LoadableViewss" customModuleProvider="target">
12+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="iOSTestableView" customModule="Example" customModuleProvider="target">
1313
<connections>
1414
<outlet property="label" destination="Xb7-wL-oSh" id="Zfm-IV-vR4"/>
1515
</connections>
@@ -20,7 +20,7 @@
2020
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2121
<subviews>
2222
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Xb7-wL-oSh">
23-
<rect key="frame" x="166.5" y="323" width="42" height="21"/>
23+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
2424
<fontDescription key="fontDescription" type="system" pointSize="17"/>
2525
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
2626
<nil key="highlightedColor"/>
@@ -29,7 +29,11 @@
2929
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3030
<constraints>
3131
<constraint firstItem="Xb7-wL-oSh" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="centerY" id="PiK-OV-nOU"/>
32+
<constraint firstAttribute="trailing" secondItem="Xb7-wL-oSh" secondAttribute="trailing" id="a6G-Ma-Ygb"/>
3233
<constraint firstItem="Xb7-wL-oSh" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="bBE-hb-jfV"/>
34+
<constraint firstItem="Xb7-wL-oSh" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" id="bTd-sT-uKg"/>
35+
<constraint firstItem="Xb7-wL-oSh" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" id="rgt-PV-igG"/>
36+
<constraint firstAttribute="bottom" secondItem="Xb7-wL-oSh" secondAttribute="bottom" id="xuQ-5d-9Vh"/>
3337
</constraints>
3438
</view>
3539
</objects>

Tests/LoadableViewsTests/LoadableViewTestCase.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ class LoadableViewTestCase: XCTestCase {
4040

4141
XCTAssertNotNil(control.textField)
4242
}
43+
44+
func testLoadableViewDoesNotHaveAFrame() {
45+
let view = iOSTestableView()
46+
XCTAssertEqual(view.frame, .zero)
47+
}
48+
49+
func testLoadableViewCanCalculateItsFrameUsingCompressedOrExpandedLayout() {
50+
let compact = iOSTestableView().compressedLayout()
51+
let expanded = iOSTestableView().expandedLayout()
52+
let system = iOSTestableView().systemLayout(fittingSize: UIView.layoutFittingCompressedSize,
53+
horizontalPriority: UILayoutPriority.fittingSizeLevel,
54+
verticalPriority: UILayoutPriority.fittingSizeLevel)
55+
XCTAssertNotEqual(compact.frame, .zero)
56+
XCTAssertNotEqual(expanded.frame, .zero)
57+
XCTAssertNotEqual(system.frame, .zero)
58+
}
4359
}
4460

4561
class LoadableViewSetupNibTestCase : XCTestCase {

0 commit comments

Comments
 (0)