Skip to content

Commit

Permalink
Adds convenience sizing methods for views that use AutoLayout and are…
Browse files Browse the repository at this point in the history
… capable of calculating their sizes.
  • Loading branch information
DenTelezhkin committed Apr 6, 2019
1 parent 124f142 commit 6d834cd
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.

# Next

* 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:)`.
* Dropped support for Swift 3.

## [3.2.0](https://github.com/MLSDev/LoadableViews/releases/tag/3.2.0)

* Support for Swift 5 and Xcode 10.2.
Expand Down
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ GEM
atomos (0.1.3)
babosa (1.0.2)
claide (1.0.2)
cocoapods (1.7.0.beta.2)
cocoapods (1.7.0.beta.3)
activesupport (>= 4.0.2, < 5)
claide (>= 1.0.2, < 2.0)
cocoapods-core (= 1.7.0.beta.2)
cocoapods-core (= 1.7.0.beta.3)
cocoapods-deintegrate (>= 1.0.3, < 2.0)
cocoapods-downloader (>= 1.2.2, < 2.0)
cocoapods-plugins (>= 1.0.0, < 2.0)
Expand All @@ -30,12 +30,12 @@ GEM
molinillo (~> 0.6.6)
nap (~> 1.0)
ruby-macho (~> 1.4)
xcodeproj (>= 1.8.1, < 2.0)
cocoapods-core (1.7.0.beta.2)
xcodeproj (>= 1.8.2, < 2.0)
cocoapods-core (1.7.0.beta.3)
activesupport (>= 4.0.2, < 6)
fuzzy_match (~> 2.0.4)
nap (~> 1.0)
cocoapods-deintegrate (1.0.3)
cocoapods-deintegrate (1.0.4)
cocoapods-downloader (1.2.2)
cocoapods-plugins (1.0.0)
nap
Expand Down Expand Up @@ -142,7 +142,7 @@ GEM
memoist (0.16.0)
mime-types (3.2.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2018.0812)
mime-types-data (3.2019.0331)
mini_magick (4.5.1)
minitest (5.11.3)
molinillo (0.6.6)
Expand All @@ -153,7 +153,7 @@ GEM
nap (1.1.0)
naturally (2.2.0)
netrc (0.11.0)
octokit (4.13.0)
octokit (4.14.0)
sawyer (~> 0.8.0, >= 0.5.3)
os (1.0.0)
plist (3.5.0)
Expand Down Expand Up @@ -195,7 +195,7 @@ GEM
unf_ext (0.0.7.5)
unicode-display_width (1.5.0)
word_wrap (1.0.0)
xcodeproj (1.8.1)
xcodeproj (1.8.2)
CFPropertyList (>= 2.3.3, < 4.0)
atomos (~> 0.1.3)
claide (>= 1.0.2, < 2.0)
Expand Down
2 changes: 1 addition & 1 deletion LoadableViews.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Pod::Spec.new do |s|
s.social_media_url = "https://twitter.com/mlsdevcom"
s.ios.deployment_target = "8.0"
s.tvos.deployment_target = "9.0"
s.swift_versions = ['3.2', '4.0', '4.2', '5.0']
s.swift_versions = ['4.0', '4.2', '5.0']
s.source = { git: "https://github.com/MLSDev/LoadableViews.git", tag: s.version.to_s }
s.source_files = "Source/*.swift"
s.framework = "UIKit"
Expand Down
35 changes: 35 additions & 0 deletions Source/LoadableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,41 @@ extension NibLoadableProtocol {
}
}

extension NibLoadableProtocol where Self: UIView {

/// Sets the frame of the view to result of `systemLayoutSizeFitting` method call with `UIView.layoutFittingCompressedSize` parameter.
///
/// - Returns: loadable view
public func compressedLayout() -> Self {
frame.size = systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
return self
}

/// Sets the frame of the view to result of `systemLayoutSizeFitting` method call with `UIView.layoutFittingExpandedSize` parameter.
///
/// - Returns: loadable view
public func expandedLayout() -> Self {
frame.size = systemLayoutSizeFitting(UIView.layoutFittingExpandedSize)
return self
}

/// Sets the frame of the view to result of `systemLayoutSizeFitting` method call with provided parameters.
///
/// - Parameters:
/// - fittingSize: fittingSize to be passed to `systemLayoutSizeFitting` method.
/// - horizontalPriority: horizontal priority to be passed to `systemLayoutSizeFitting` method.
/// - verticalPriority: vertical priority to be passed to `systemLayoutSizeFitting` method.
/// - Returns: loadable view
public func systemLayout(fittingSize: CGSize,
horizontalPriority: UILayoutPriority,
verticalPriority: UILayoutPriority) -> Self {
frame.size = systemLayoutSizeFitting(fittingSize,
withHorizontalFittingPriority: horizontalPriority,
verticalFittingPriority: verticalPriority)
return self
}
}

/// UIView subclass, that can be loaded into different xib or storyboard by simply referencing it's class.
open class LoadableView: UIView, NibLoadableProtocol {

Expand Down
12 changes: 8 additions & 4 deletions TestFixtures/iOSTestableView.xib
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="iOSTestableView" customModule="LoadableViewss" customModuleProvider="target">
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="iOSTestableView" customModule="Example" customModuleProvider="target">
<connections>
<outlet property="label" destination="Xb7-wL-oSh" id="Zfm-IV-vR4"/>
</connections>
Expand All @@ -20,7 +20,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<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">
<rect key="frame" x="166.5" y="323" width="42" height="21"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
Expand All @@ -29,7 +29,11 @@
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="Xb7-wL-oSh" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="centerY" id="PiK-OV-nOU"/>
<constraint firstAttribute="trailing" secondItem="Xb7-wL-oSh" secondAttribute="trailing" id="a6G-Ma-Ygb"/>
<constraint firstItem="Xb7-wL-oSh" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="bBE-hb-jfV"/>
<constraint firstItem="Xb7-wL-oSh" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" id="bTd-sT-uKg"/>
<constraint firstItem="Xb7-wL-oSh" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" id="rgt-PV-igG"/>
<constraint firstAttribute="bottom" secondItem="Xb7-wL-oSh" secondAttribute="bottom" id="xuQ-5d-9Vh"/>
</constraints>
</view>
</objects>
Expand Down
16 changes: 16 additions & 0 deletions Tests/LoadableViewsTests/LoadableViewTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ class LoadableViewTestCase: XCTestCase {

XCTAssertNotNil(control.textField)
}

func testLoadableViewDoesNotHaveAFrame() {
let view = iOSTestableView()
XCTAssertEqual(view.frame, .zero)
}

func testLoadableViewCanCalculateItsFrameUsingCompressedOrExpandedLayout() {
let compact = iOSTestableView().compressedLayout()
let expanded = iOSTestableView().expandedLayout()
let system = iOSTestableView().systemLayout(fittingSize: UIView.layoutFittingCompressedSize,
horizontalPriority: UILayoutPriority.fittingSizeLevel,
verticalPriority: UILayoutPriority.fittingSizeLevel)
XCTAssertNotEqual(compact.frame, .zero)
XCTAssertNotEqual(expanded.frame, .zero)
XCTAssertNotEqual(system.frame, .zero)
}
}

class LoadableViewSetupNibTestCase : XCTestCase {
Expand Down

0 comments on commit 6d834cd

Please sign in to comment.