Skip to content

Commit aef530e

Browse files
authored
Adds dark mode support on the Connect Button (#298)
Dark mode can now be enabled on the Connect Button by setting the button `style` property `.dark`. A new `style` option, `.dynamic`, is also available to have the Connect Button automatically adapt to light/dark trait changes.
1 parent 8b419b5 commit aef530e

8 files changed

Lines changed: 147 additions & 75 deletions

Examples/GroceryExpress/ConnectionViewController.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ class ConnectionViewController: UIViewController {
3535

3636
@IBOutlet weak var opaqueOverlay: UIView!
3737

38+
@IBOutlet weak var styleChangeButton: UIButton!
39+
40+
@IBAction func styleChangeButtonTapped(_ sender: Any) {
41+
switch connectButton.style {
42+
case .light:
43+
connectButton.style = .dark
44+
case .dark:
45+
connectButton.style = .dynamic
46+
case .dynamic:
47+
connectButton.style = .light
48+
}
49+
50+
updateStyleChangeButtonTitle()
51+
}
3852
// MARK: - Connect flow
3953

4054
private let settings = Settings()
@@ -117,10 +131,20 @@ class ConnectionViewController: UIViewController {
117131

118132
featuresStackView.isLayoutMarginsRelativeArrangement = true
119133
featuresStackView.layoutMargins = .init(top: 0, left: 20, bottom: 0, right: 20)
120-
view.backgroundColor = .white
134+
if #available(iOS 13.0, *) {
135+
view.backgroundColor = .tertiarySystemBackground
136+
} else {
137+
view.backgroundColor = .white
138+
}
121139
activityIndicator.color = .black
122140

123141
update(with: displayInformation)
142+
updateStyleChangeButtonTitle()
143+
}
144+
145+
private func updateStyleChangeButtonTitle() {
146+
styleChangeButton.setTitle("Change style (\(connectButton.style))", for: .normal)
147+
styleChangeButton.setTitle("Change style (\(connectButton.style))", for: .selected)
124148
}
125149

126150
private func update(with display: DisplayInformation) {

Examples/GroceryExpress/Info.plist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@
7777
<string>UIInterfaceOrientationLandscapeLeft</string>
7878
<string>UIInterfaceOrientationLandscapeRight</string>
7979
</array>
80-
<key>UIUserInterfaceStyle</key>
81-
<string>Light</string>
8280
<key>ITSAppUsesNonExemptEncryption</key>
8381
<false/>
8482
</dict>

Examples/GroceryExpress/Main.storyboard

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="QrA-rv-z6d">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21507" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="QrA-rv-z6d">
33
<device id="retina5_5" orientation="portrait" appearance="light"/>
44
<dependencies>
55
<deployment identifier="iOS"/>
6-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21505"/>
77
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
88
<capability name="System colors in document resources" minToolsVersion="11.0"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
@@ -192,7 +192,7 @@
192192
<rect key="frame" x="0.0" y="308.66666666666669" width="354" height="151.66666666666669"/>
193193
<subviews>
194194
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="User is not logged into IFTTT" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Wtn-AT-fpa">
195-
<rect key="frame" x="60.333333333333329" y="0.0" width="233.66666666666669" height="20.333333333333332"/>
195+
<rect key="frame" x="60.333333333333329" y="0.0" width="233.33333333333337" height="20.333333333333332"/>
196196
<fontDescription key="fontDescription" type="boldSystem" pointSize="17"/>
197197
<nil key="textColor"/>
198198
<nil key="highlightedColor"/>
@@ -456,8 +456,17 @@
456456
</constraints>
457457
<edgeInsets key="layoutMargins" top="0.0" left="20" bottom="0.0" right="20"/>
458458
</stackView>
459+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ZMe-Cy-eJC">
460+
<rect key="frame" x="169.66666666666666" y="670" width="75" height="35"/>
461+
<state key="normal" title="Button"/>
462+
<buttonConfiguration key="configuration" style="tinted" title="Button"/>
463+
<connections>
464+
<action selector="styleChangeButtonTapped:" destination="Qpl-UT-38S" eventType="touchUpInside" id="Ijp-eQ-aBF"/>
465+
</connections>
466+
</button>
459467
</subviews>
460468
<constraints>
469+
<constraint firstItem="ZMe-Cy-eJC" firstAttribute="centerX" secondItem="CS4-C0-5nt" secondAttribute="centerX" id="0wm-EN-Kf3"/>
461470
<constraint firstItem="K6a-ig-8eM" firstAttribute="leading" secondItem="CS4-C0-5nt" secondAttribute="leading" constant="16" id="2hy-qG-IaQ"/>
462471
<constraint firstItem="olx-BK-h7F" firstAttribute="leading" secondItem="CS4-C0-5nt" secondAttribute="leading" id="2oY-Ud-PTM"/>
463472
<constraint firstItem="olx-BK-h7F" firstAttribute="top" secondItem="8Tc-IV-1W8" secondAttribute="bottom" constant="30" id="37u-Rs-36t"/>
@@ -473,6 +482,7 @@
473482
<constraint firstAttribute="trailing" secondItem="8Tc-IV-1W8" secondAttribute="trailing" id="Zpv-Z1-XBj"/>
474483
<constraint firstItem="XJV-id-Dha" firstAttribute="leading" secondItem="8Tc-IV-1W8" secondAttribute="leading" id="bKC-Mz-ntd"/>
475484
<constraint firstAttribute="trailing" secondItem="K6a-ig-8eM" secondAttribute="trailing" constant="16" id="ghb-EQ-8Ob"/>
485+
<constraint firstAttribute="bottom" secondItem="ZMe-Cy-eJC" secondAttribute="bottom" constant="40" id="h0z-zk-lzc"/>
476486
<constraint firstAttribute="trailing" secondItem="bag-Y6-owB" secondAttribute="trailing" id="h4L-el-I1e"/>
477487
<constraint firstItem="8Tc-IV-1W8" firstAttribute="width" secondItem="CS4-C0-5nt" secondAttribute="width" id="hxt-t2-hCd"/>
478488
<constraint firstItem="XJV-id-Dha" firstAttribute="bottom" secondItem="8Tc-IV-1W8" secondAttribute="bottom" id="vRH-Mg-9yC"/>
@@ -496,6 +506,7 @@
496506
<outlet property="connectButton" destination="olx-BK-h7F" id="ktb-dg-ZIw"/>
497507
<outlet property="featuresStackView" destination="bag-Y6-owB" id="sqF-6k-JSJ"/>
498508
<outlet property="opaqueOverlay" destination="XJV-id-Dha" id="nZb-gG-Ypt"/>
509+
<outlet property="styleChangeButton" destination="ZMe-Cy-eJC" id="DDb-BE-cJA"/>
499510
<outlet property="subtitle" destination="K6a-ig-8eM" id="3CG-ct-550"/>
500511
</connections>
501512
</viewController>

IFTTT SDK/ConnectButton+Constants.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,20 @@ extension ConnectButton {
3535
static let mediumGrey = UIColor(hex: 0x666666)
3636
static let grey = UIColor(hex: 0x414141)
3737
static let border = UIColor(white: 1, alpha: 0.32)
38+
static let almostBlack = UIColor(hex: 0x222222)
39+
40+
static func dynamicColor(light: UIColor, dark: UIColor) -> UIColor {
41+
if #available(iOS 13, *) {
42+
return UIColor { (UITraitCollection: UITraitCollection) -> UIColor in
43+
if UITraitCollection.userInterfaceStyle == .dark {
44+
return dark
45+
} else {
46+
return light
47+
}
48+
}
49+
} else {
50+
return light
51+
}
52+
}
3853
}
3954
}

IFTTT SDK/ConnectButton+LabelValue.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ extension ConnectButton {
2020

2121
/// The attributed text of the label.
2222
case attributed(NSAttributedString)
23+
24+
/// The text color of the label.
25+
case textColor(UIColor)
2326

2427
/// Updates the label with the value of the enum.
2528
///
@@ -33,6 +36,8 @@ extension ConnectButton {
3336
label.text = text
3437
case .attributed(let text):
3538
label.attributedText = text
39+
case .textColor(let color):
40+
label.textColor = color
3641
}
3742
}
3843

@@ -52,6 +57,8 @@ extension ConnectButton {
5257
return lhs == rhs
5358
case (.attributed(let lhs), .attributed(let rhs)):
5459
return lhs == rhs
60+
case (.textColor(let lhs), .textColor(let rhs)):
61+
return lhs == rhs
5562
default:
5663
return false
5764
}

IFTTT SDK/ConnectButton+ProgressBar.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ extension ConnectButton {
2121

2222
private let track = UIView()
2323
private let bar = PassthroughView()
24+
25+
private var defaultColor: UIColor {
26+
return Color.dynamicColor(
27+
light: Color.grey,
28+
dark: Color.lightGrey
29+
)
30+
}
2431

2532
/// Configures the progress bar background with the optionally provided `Service`.
2633
///
2734
/// - Parameter service: An optional `Service` to set the backgrund color to.
2835
func configure(with service: Service?) {
29-
bar.backgroundColor = service?.brandColor.contrasting() ?? Color.grey
36+
bar.backgroundColor = service?.brandColor.contrasting() ?? defaultColor
3037
}
3138

3239
private func update() {

IFTTT SDK/ConnectButton+Style.swift

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ extension ConnectButton {
1616

1717
/// Style the button for a white background
1818
case light
19+
20+
/// Style the button for a dark background
21+
case dark
22+
23+
/// Style the button for with dynamic colors:
24+
/// When the user device is on light mode, the style used is equivalent to `light`.
25+
/// When on dark mode, the style is equivalent to `dark`.
26+
///
27+
/// On iOS versions before 13.0, this style is the same as `light`.
28+
case dynamic
1929

2030
struct Font {
2131
static let connect = UIFont(name: "AvenirNext-Bold", size: 22)!
@@ -24,18 +34,28 @@ extension ConnectButton {
2434

2535
/// The color to use for the footer based on the style.
2636
var footerColor: UIColor {
37+
return UIColor(hex: 0x999999)
38+
}
39+
40+
/// The color to use for the button background based on the style
41+
var buttonBackgroundColor: UIColor {
42+
return colors(light: Color.almostBlack, dark: .white)
43+
}
44+
45+
/// The color to use for the button text based on the style
46+
var textColor: UIColor {
47+
return colors(light: .white, dark: .black)
48+
}
49+
50+
private func colors(light: UIColor, dark: UIColor) -> UIColor {
2751
switch self {
2852
case .light:
29-
return lightColorFooter()
53+
return light
54+
case .dark:
55+
return dark
56+
case .dynamic:
57+
return Color.dynamicColor(light: light, dark: dark)
3058
}
3159
}
32-
33-
private func lightColorFooter() -> UIColor {
34-
return UIColor(white: 0, alpha: 0.32)
35-
}
36-
37-
private func darkColorFooter() -> UIColor {
38-
return UIColor(white: 1, alpha: 0.32)
39-
}
4060
}
4161
}

0 commit comments

Comments
 (0)