Skip to content

Commit 4dfe290

Browse files
committed
Add automatic margin adjustments
1 parent ca76b37 commit 4dfe290

8 files changed

+168
-73
lines changed

SwiftMessages.xcodeproj/project.pbxproj

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
86AAF8181D54F0650031EE32 /* PassthroughView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AAF8171D54F0650031EE32 /* PassthroughView.swift */; };
2828
86AAF81A1D54F0850031EE32 /* PassthroughWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AAF8191D54F0850031EE32 /* PassthroughWindow.swift */; };
2929
86AAF81C1D551FE70031EE32 /* UIViewController+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AAF81B1D551FE60031EE32 /* UIViewController+Utils.swift */; };
30+
86AAF81E1D5549680031EE32 /* MarginAdjustable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AAF81D1D5549680031EE32 /* MarginAdjustable.swift */; };
31+
86AAF8261D5553170031EE32 /* StatusLine.xib in Resources */ = {isa = PBXBuildFile; fileRef = 86AAF8251D5553170031EE32 /* StatusLine.xib */; };
3032
/* End PBXBuildFile section */
3133

3234
/* Begin PBXContainerItemProxy section */
@@ -73,6 +75,8 @@
7375
86AAF8171D54F0650031EE32 /* PassthroughView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PassthroughView.swift; sourceTree = "<group>"; };
7476
86AAF8191D54F0850031EE32 /* PassthroughWindow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PassthroughWindow.swift; sourceTree = "<group>"; };
7577
86AAF81B1D551FE60031EE32 /* UIViewController+Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+Utils.swift"; sourceTree = "<group>"; };
78+
86AAF81D1D5549680031EE32 /* MarginAdjustable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MarginAdjustable.swift; sourceTree = "<group>"; };
79+
86AAF8251D5553170031EE32 /* StatusLine.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = StatusLine.xib; sourceTree = "<group>"; };
7680
/* End PBXFileReference section */
7781

7882
/* Begin PBXFrameworksBuildPhase section */
@@ -104,6 +108,7 @@
104108
isa = PBXGroup;
105109
children = (
106110
864495551D4F7C390056EB2A /* Identifiable.swift */,
111+
86AAF81D1D5549680031EE32 /* MarginAdjustable.swift */,
107112
);
108113
name = Base;
109114
sourceTree = "<group>";
@@ -175,18 +180,11 @@
175180
isa = PBXGroup;
176181
children = (
177182
867E21881D4D06D900594A41 /* Images.xcassets */,
178-
867E218B1D4D3CD700594A41 /* Default */,
179-
);
180-
name = "Message Views";
181-
sourceTree = "<group>";
182-
};
183-
867E218B1D4D3CD700594A41 /* Default */ = {
184-
isa = PBXGroup;
185-
children = (
186183
867E21821D4D025200594A41 /* MessageView.swift */,
187184
867E21861D4D04C600594A41 /* MessageView.xib */,
185+
86AAF8251D5553170031EE32 /* StatusLine.xib */,
188186
);
189-
name = Default;
187+
name = "Message Views";
190188
sourceTree = "<group>";
191189
};
192190
867E218E1D4D3DFD00594A41 /* Internal */ = {
@@ -309,6 +307,7 @@
309307
buildActionMask = 2147483647;
310308
files = (
311309
867E21871D4D04C600594A41 /* MessageView.xib in Resources */,
310+
86AAF8261D5553170031EE32 /* StatusLine.xib in Resources */,
312311
867E21891D4D06D900594A41 /* Images.xcassets in Resources */,
313312
867E215D1D4D01D500594A41 /* LaunchScreen.storyboard in Resources */,
314313
867E215A1D4D01D500594A41 /* Assets.xcassets in Resources */,
@@ -348,6 +347,7 @@
348347
8644955D1D4FAF7C0056EB2A /* WindowViewController.swift in Sources */,
349348
867E21531D4D01D500594A41 /* AppDelegate.swift in Sources */,
350349
867E21901D4D3E5E00594A41 /* UIView+Message.swift in Sources */,
350+
86AAF81E1D5549680031EE32 /* MarginAdjustable.swift in Sources */,
351351
86AAF81C1D551FE70031EE32 /* UIViewController+Utils.swift in Sources */,
352352
8644955B1D4FA2690056EB2A /* Presentable.swift in Sources */,
353353
);

SwiftMessages/GalleryTableViewController.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class GalleryTableViewController: UITableViewController {
1414
let style = MessageView.errorConfiguration()
1515
let content = MessageView.contentConfiguration(title: "My Title", body: "This is my body message.")
1616
var config = Configuration<MessageView>(viewConfigurations: [style, content])
17-
// config.presentationContext = .Window(windowLevel: UIWindowLevelStatusBar)
18-
config.presentationContext = .Automatic
17+
config.presentationContext = .Window(windowLevel: UIWindowLevelStatusBar)
18+
config.preferredStatusBarStyle = .LightContent
19+
// config.presentationContext = .Automatic
1920
config.presentationStyle = .Top
21+
config.nibName = "StatusLine"
2022
try! config.show()
2123
}
2224
}

SwiftMessages/MarginAdjustable.swift

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// MarginAdjustable.swift
3+
// SwiftMessages
4+
//
5+
// Created by Tim Moose on 8/5/16.
6+
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
public protocol MarginAdjustable {
12+
var bounceAnimationOffset: CGFloat { get set }
13+
var statusBarOffset: CGFloat { get set }
14+
}

SwiftMessages/MessageView.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import UIKit
1010

11-
public class MessageView: UIView, Identifiable {
11+
public class MessageView: UIView, Identifiable, MarginAdjustable {
1212

1313
/*
1414
MARK: - IB outlets
@@ -89,6 +89,7 @@ public class MessageView: UIView, Identifiable {
8989
self.iconLabel?.text = nil
9090
self.button?.setImage(nil, forState: .Normal)
9191
self.button?.setTitle(nil, forState: .Normal)
92+
self.layoutMargins = UIEdgeInsetsZero
9293
}
9394

9495
/*
@@ -98,4 +99,12 @@ public class MessageView: UIView, Identifiable {
9899
public var identity: String {
99100
return "MessageView:title=\(titleLabel?.text), body=\(bodyLabel?.text)"
100101
}
102+
103+
/*
104+
MARK: - MarginAdjustable
105+
*/
106+
107+
@IBInspectable public var bounceAnimationOffset: CGFloat = 5.0
108+
109+
@IBInspectable public var statusBarOffset: CGFloat = 20.0
101110
}

SwiftMessages/MessageView.xib

+62-40
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,95 @@
33
<dependencies>
44
<deployment identifier="iOS"/>
55
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
6+
<capability name="Alignment constraints with different attributes" minToolsVersion="5.1"/>
7+
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
68
</dependencies>
79
<objects>
810
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
911
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
1012
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="MessageView" customModule="SwiftMessages" customModuleProvider="target">
11-
<rect key="frame" x="0.0" y="0.0" width="600" height="80"/>
13+
<rect key="frame" x="0.0" y="0.0" width="600" height="103"/>
1214
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1315
<subviews>
14-
<imageView userInteractionEnabled="NO" contentMode="center" horizontalHuggingPriority="252" verticalHuggingPriority="251" image="errorIcon" translatesAutoresizingMaskIntoConstraints="NO" id="PbA-QP-XpA" userLabel="imageIcon">
15-
<rect key="frame" x="20" y="21" width="32" height="39"/>
16-
</imageView>
17-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="😬" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1Bu-DY-qZo" userLabel="iconLabel">
18-
<rect key="frame" x="20" y="21" width="32" height="39"/>
19-
<fontDescription key="fontDescription" type="system" pointSize="32"/>
20-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
21-
<nil key="highlightedColor"/>
22-
</label>
23-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="90y-Jf-WFn" userLabel="Text container">
24-
<rect key="frame" x="72" y="21" width="508" height="39"/>
16+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="FRd-Nk-eVT" userLabel="Container">
17+
<rect key="frame" x="8" y="8" width="584" height="87"/>
2518
<subviews>
26-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" ambiguous="YES" misplaced="YES" text="Title" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="8T6-4T-ytS">
27-
<rect key="frame" x="0.0" y="0.0" width="518" height="20"/>
28-
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/>
29-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
30-
<nil key="highlightedColor"/>
31-
</label>
32-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" ambiguous="YES" misplaced="YES" text="Message Body" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dFP-4Z-N2a">
33-
<rect key="frame" x="0.0" y="19" width="518" height="20"/>
34-
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
19+
<imageView userInteractionEnabled="NO" contentMode="center" horizontalHuggingPriority="252" verticalHuggingPriority="251" image="errorIcon" translatesAutoresizingMaskIntoConstraints="NO" id="PbA-QP-XpA" userLabel="imageIcon">
20+
<rect key="frame" x="20" y="34.5" width="32" height="18"/>
21+
</imageView>
22+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="😬" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1Bu-DY-qZo" userLabel="iconLabel">
23+
<rect key="frame" x="20" y="24.5" width="32" height="38.5"/>
24+
<fontDescription key="fontDescription" type="system" pointSize="32"/>
3525
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
3626
<nil key="highlightedColor"/>
3727
</label>
28+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="90y-Jf-WFn" userLabel="Text container">
29+
<rect key="frame" x="72" y="24" width="492" height="39.5"/>
30+
<subviews>
31+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Title" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="8T6-4T-ytS">
32+
<rect key="frame" x="0.0" y="0.0" width="492" height="19.5"/>
33+
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/>
34+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
35+
<nil key="highlightedColor"/>
36+
</label>
37+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Message Body" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dFP-4Z-N2a">
38+
<rect key="frame" x="0.0" y="19.5" width="492" height="20"/>
39+
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
40+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
41+
<nil key="highlightedColor"/>
42+
</label>
43+
</subviews>
44+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
45+
<constraints>
46+
<constraint firstItem="dFP-4Z-N2a" firstAttribute="leading" secondItem="90y-Jf-WFn" secondAttribute="leading" id="6jt-SY-dxJ"/>
47+
<constraint firstAttribute="trailing" secondItem="8T6-4T-ytS" secondAttribute="trailing" id="9Yl-uR-LtH"/>
48+
<constraint firstItem="8T6-4T-ytS" firstAttribute="top" secondItem="90y-Jf-WFn" secondAttribute="top" id="ffB-Ua-jSj"/>
49+
<constraint firstAttribute="trailing" secondItem="dFP-4Z-N2a" secondAttribute="trailing" id="gAw-js-SaN"/>
50+
<constraint firstItem="dFP-4Z-N2a" firstAttribute="top" secondItem="8T6-4T-ytS" secondAttribute="bottom" id="gKY-kH-CMA"/>
51+
<constraint firstAttribute="bottom" secondItem="dFP-4Z-N2a" secondAttribute="bottom" id="rww-g1-SH3"/>
52+
<constraint firstItem="8T6-4T-ytS" firstAttribute="leading" secondItem="90y-Jf-WFn" secondAttribute="leading" id="ypQ-6p-Wus"/>
53+
</constraints>
54+
</view>
3855
</subviews>
3956
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
4057
<constraints>
41-
<constraint firstItem="dFP-4Z-N2a" firstAttribute="leading" secondItem="90y-Jf-WFn" secondAttribute="leading" id="6jt-SY-dxJ"/>
42-
<constraint firstAttribute="trailing" secondItem="8T6-4T-ytS" secondAttribute="trailing" id="9Yl-uR-LtH"/>
43-
<constraint firstItem="8T6-4T-ytS" firstAttribute="top" secondItem="90y-Jf-WFn" secondAttribute="top" id="ffB-Ua-jSj"/>
44-
<constraint firstAttribute="trailing" secondItem="dFP-4Z-N2a" secondAttribute="trailing" id="gAw-js-SaN"/>
45-
<constraint firstItem="dFP-4Z-N2a" firstAttribute="top" secondItem="8T6-4T-ytS" secondAttribute="bottom" id="gKY-kH-CMA"/>
46-
<constraint firstAttribute="bottom" secondItem="dFP-4Z-N2a" secondAttribute="bottom" id="rww-g1-SH3"/>
47-
<constraint firstItem="8T6-4T-ytS" firstAttribute="leading" secondItem="90y-Jf-WFn" secondAttribute="leading" id="ypQ-6p-Wus"/>
58+
<constraint firstAttribute="trailing" secondItem="90y-Jf-WFn" secondAttribute="trailing" constant="20" id="7Ne-l9-H5F"/>
59+
<constraint firstItem="1Bu-DY-qZo" firstAttribute="leading" secondItem="FRd-Nk-eVT" secondAttribute="leading" constant="20" id="BDi-O3-J8K"/>
60+
<constraint firstItem="90y-Jf-WFn" firstAttribute="top" relation="greaterThanOrEqual" secondItem="FRd-Nk-eVT" secondAttribute="top" constant="20" id="J2N-sf-z4p"/>
61+
<constraint firstItem="90y-Jf-WFn" firstAttribute="centerY" secondItem="FRd-Nk-eVT" secondAttribute="centerYWithinMargins" id="JsW-xH-y9M"/>
62+
<constraint firstItem="PbA-QP-XpA" firstAttribute="top" relation="greaterThanOrEqual" secondItem="FRd-Nk-eVT" secondAttribute="top" constant="20" id="Xfo-hE-Qlz"/>
63+
<constraint firstItem="PbA-QP-XpA" firstAttribute="centerY" secondItem="FRd-Nk-eVT" secondAttribute="centerYWithinMargins" id="aVF-AK-JLI"/>
64+
<constraint firstItem="1Bu-DY-qZo" firstAttribute="top" relation="greaterThanOrEqual" secondItem="FRd-Nk-eVT" secondAttribute="top" constant="20" id="aVr-8D-CoE"/>
65+
<constraint firstItem="90y-Jf-WFn" firstAttribute="leading" secondItem="1Bu-DY-qZo" secondAttribute="trailing" constant="20" id="h5O-7j-cl7"/>
66+
<constraint firstItem="1Bu-DY-qZo" firstAttribute="centerY" secondItem="FRd-Nk-eVT" secondAttribute="centerYWithinMargins" id="i4c-fN-6x2"/>
67+
<constraint firstItem="PbA-QP-XpA" firstAttribute="leading" secondItem="FRd-Nk-eVT" secondAttribute="leading" constant="20" id="nVS-8Q-AUb"/>
68+
<constraint firstItem="90y-Jf-WFn" firstAttribute="leading" secondItem="PbA-QP-XpA" secondAttribute="trailing" constant="20" id="odz-Ah-f27"/>
4869
</constraints>
4970
</view>
5071
</subviews>
5172
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
5273
<constraints>
53-
<constraint firstItem="90y-Jf-WFn" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="centerY" id="2YJ-Sz-f1A"/>
54-
<constraint firstItem="1Bu-DY-qZo" firstAttribute="centerY" secondItem="PbA-QP-XpA" secondAttribute="centerY" id="3yq-4r-353"/>
55-
<constraint firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="90y-Jf-WFn" secondAttribute="bottom" constant="20" id="7oP-RO-6Ys"/>
56-
<constraint firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="PbA-QP-XpA" secondAttribute="bottom" constant="20" id="HFh-ii-vwL"/>
57-
<constraint firstItem="PbA-QP-XpA" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" id="OY1-5P-0QL"/>
58-
<constraint firstItem="1Bu-DY-qZo" firstAttribute="width" secondItem="PbA-QP-XpA" secondAttribute="width" id="S20-Mo-Pnv"/>
59-
<constraint firstAttribute="trailing" secondItem="90y-Jf-WFn" secondAttribute="trailing" constant="20" id="UI8-aO-lPt"/>
60-
<constraint firstItem="1Bu-DY-qZo" firstAttribute="height" secondItem="PbA-QP-XpA" secondAttribute="height" id="WsQ-Iu-9nL"/>
61-
<constraint firstItem="1Bu-DY-qZo" firstAttribute="centerX" secondItem="PbA-QP-XpA" secondAttribute="centerX" id="oLA-XN-Hma"/>
62-
<constraint firstItem="90y-Jf-WFn" firstAttribute="leading" secondItem="PbA-QP-XpA" secondAttribute="trailing" constant="20" id="rea-TG-jfC"/>
63-
<constraint firstItem="PbA-QP-XpA" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="centerY" id="ujI-Pi-Ewy"/>
74+
<constraint firstAttribute="bottomMargin" secondItem="FRd-Nk-eVT" secondAttribute="bottom" id="2A8-CG-Ai9"/>
75+
<constraint firstAttribute="trailingMargin" secondItem="FRd-Nk-eVT" secondAttribute="trailing" id="9Ph-3b-gI9"/>
76+
<constraint firstItem="FRd-Nk-eVT" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="topMargin" id="VKO-GB-Fgv"/>
77+
<constraint firstItem="FRd-Nk-eVT" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leadingMargin" id="yTo-ES-5GZ"/>
6478
</constraints>
6579
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
80+
<userDefinedRuntimeAttributes>
81+
<userDefinedRuntimeAttribute type="number" keyPath="bounceAnimationOffset">
82+
<real key="value" value="5"/>
83+
</userDefinedRuntimeAttribute>
84+
<userDefinedRuntimeAttribute type="number" keyPath="statusBarOffset">
85+
<real key="value" value="8"/>
86+
</userDefinedRuntimeAttribute>
87+
</userDefinedRuntimeAttributes>
6688
<connections>
6789
<outlet property="bodyLabel" destination="dFP-4Z-N2a" id="ixI-dD-l0j"/>
6890
<outlet property="iconImage" destination="PbA-QP-XpA" id="c4A-7m-RqR"/>
6991
<outlet property="iconLabel" destination="1Bu-DY-qZo" id="hiy-dW-5Gr"/>
7092
<outlet property="titleLabel" destination="8T6-4T-ytS" id="kis-2r-nMy"/>
7193
</connections>
72-
<point key="canvasLocation" x="360" y="341"/>
94+
<point key="canvasLocation" x="360" y="352.5"/>
7395
</view>
7496
</objects>
7597
<resources>

0 commit comments

Comments
 (0)