Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionSheet customization, improved dark mode #303

Merged
merged 1 commit into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
AAA3CC80235874F3006C23B8 /* background.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = AAA3CC7E235874F2006C23B8 /* background.jpeg */; };
AAF7737821EF98970025DAB3 /* Breze.jpg in Resources */ = {isa = PBXBuildFile; fileRef = AAF7737721EF98960025DAB3 /* Breze.jpg */; };
D2515E731BC1A47F00ED606F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2515E721BC1A47F00ED606F /* AppDelegate.swift */; };
D2515E751BC1A47F00ED606F /* DemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2515E741BC1A47F00ED606F /* DemoViewController.swift */; };
D2515E781BC1A47F00ED606F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D2515E761BC1A47F00ED606F /* Main.storyboard */; };
Expand Down Expand Up @@ -49,6 +51,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
AAA3CC7E235874F2006C23B8 /* background.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = background.jpeg; sourceTree = "<group>"; };
AAF7737721EF98960025DAB3 /* Breze.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Breze.jpg; sourceTree = "<group>"; };
D2515E6F1BC1A47F00ED606F /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
D2515E721BC1A47F00ED606F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
D2515E741BC1A47F00ED606F /* DemoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -109,6 +113,8 @@
D2515E761BC1A47F00ED606F /* Main.storyboard */,
D2515E7B1BC1A47F00ED606F /* LaunchScreen.storyboard */,
D2515E7E1BC1A47F00ED606F /* Info.plist */,
AAA3CC7E235874F2006C23B8 /* background.jpeg */,
AAF7737721EF98960025DAB3 /* Breze.jpg */,
);
path = Example;
sourceTree = "<group>";
Expand Down Expand Up @@ -197,6 +203,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -232,8 +239,10 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
AAF7737821EF98970025DAB3 /* Breze.jpg in Resources */,
D2515E7D1BC1A47F00ED606F /* LaunchScreen.storyboard in Resources */,
D2515E781BC1A47F00ED606F /* Main.storyboard in Resources */,
AAA3CC80235874F3006C23B8 /* background.jpeg in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
39 changes: 31 additions & 8 deletions Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13189.4" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="uvy-DI-gcx">
<device id="retina4_0" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="uvy-DI-gcx">
<device id="retina4_0" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14824"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16086"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -35,6 +33,10 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<imageView key="tableFooterView" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="background.jpeg" id="w2d-6w-2Eu">
<rect key="frame" x="0.0" y="464" width="320" height="400"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<sections>
<tableViewSection id="tPU-Xi-kFY">
<cells>
Expand Down Expand Up @@ -91,7 +93,7 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Title" textAlignment="natural" minimumFontSize="17" clearButtonMode="always" translatesAutoresizingMaskIntoConstraints="NO" id="P5A-kb-4UT">
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="Title" placeholder="Title" textAlignment="natural" minimumFontSize="17" clearButtonMode="always" translatesAutoresizingMaskIntoConstraints="NO" id="P5A-kb-4UT">
<rect key="frame" x="16" y="11" width="288" height="22"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
Expand All @@ -112,7 +114,7 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Message" textAlignment="natural" minimumFontSize="17" clearButtonMode="always" translatesAutoresizingMaskIntoConstraints="NO" id="lbO-L6-mPt">
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="Message" placeholder="Message" textAlignment="natural" minimumFontSize="17" clearButtonMode="always" translatesAutoresizingMaskIntoConstraints="NO" id="lbO-L6-mPt">
<rect key="frame" x="16" y="11" width="288" height="22"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
Expand Down Expand Up @@ -235,7 +237,7 @@
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="749" text="Content" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.80000000000000004" translatesAutoresizingMaskIntoConstraints="NO" id="qOh-v5-Ed4">
<rect key="frame" x="16" y="12" width="13" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
Expand Down Expand Up @@ -490,6 +492,24 @@ running Xcode 7's UI tests</string>
<rect key="frame" x="16" y="0.0" width="288" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" textLabel="fc2-rq-c21" style="IBUITableViewCellStyleDefault" id="kpJ-pc-qAK">
<rect key="frame" x="0.0" y="607" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="kpJ-pc-qAK" id="Aab-Tb-LNq">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="action sheet cell customization" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="fc2-rq-c21">
<rect key="frame" x="16" y="0.0" width="288" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
Expand Down Expand Up @@ -529,4 +549,7 @@ running Xcode 7's UI tests</string>
<point key="canvasLocation" x="1834" y="438"/>
</scene>
</scenes>
<resources>
<image name="background.jpeg" width="3024" height="4032"/>
</resources>
</document>
Binary file added Example/Breze.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions Example/DemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ final class DemoViewController: UITableViewController {
}

private func presentSDCAlertController() {
let title = self.titleTextField.content
let message = self.messageTextField.content
let title = self.titleTextField.text
let message = self.messageTextField.text
let style = AlertControllerStyle(rawValue: self.styleControl.selectedSegmentIndex)!
let alert = AlertController(title: title, message: message, preferredStyle: style)

Expand Down Expand Up @@ -70,8 +70,6 @@ final class DemoViewController: UITableViewController {
switchControl.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
switchControl.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true

alert.message = "Disable switch to prevent alert dismissal"

alert.shouldDismissHandler = { [unowned switchControl] _ in
return switchControl.isOn
}
Expand Down
41 changes: 36 additions & 5 deletions Example/TestsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,53 @@ class TestsViewController: UITableViewController {
action.accessibilityIdentifier = "button"
alert.addAction(action)
alert.present()

case 11:
let alert = AlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
let contentView = alert.contentView
let spinner = UIActivityIndicatorView(style: .gray)
let spinner: UIActivityIndicatorView
if #available(iOS 13.0, *) {
spinner = UIActivityIndicatorView(style: .medium)
} else {
spinner = UIActivityIndicatorView(style: .gray)
}
spinner.translatesAutoresizingMaskIntoConstraints = false
spinner.startAnimating()
contentView.addSubview(spinner)
spinner.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
spinner.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
spinner.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
spinner.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true
spinner.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10).isActive = true
let action = AlertAction(title: "Cancel", style: .normal)
action.accessibilityIdentifier = "cancel"
alert.addAction(action)
alert.present()


case 12:
let alert = AlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
let action = AlertAction(title: "Custom Action", style: .normal)
action.imageView = UIImageView(image: UIImage(named: "Breze.jpg"))
let spinner: UIActivityIndicatorView
if #available(iOS 13.0, *) {
spinner = UIActivityIndicatorView(style: .medium)
} else {
spinner = UIActivityIndicatorView(style: .gray)
}
spinner.translatesAutoresizingMaskIntoConstraints = false
spinner.startAnimating()
action.accessoryView = spinner
alert.addAction(action)

let action2 = AlertAction(title: "Custom Action 2", style: .normal)
let accessoryLabel = UILabel()
accessoryLabel.text = "✓"
accessoryLabel.font = UIFont.systemFont(ofSize: 20)
accessoryLabel.textColor = view.tintColor
action2.accessoryView = accessoryLabel
alert.addAction(action2)

alert.addAction(AlertAction(title: "Cancel", style: .preferred))
alert.present()

default: break
}
}
Expand Down
Binary file added Example/background.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 38 additions & 1 deletion Source/Actions/ActionCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import UIKit

final class ActionCell: UICollectionViewCell {

@IBOutlet private(set) var stackView: UIStackView!
@IBOutlet private(set) var titleLabel: UILabel!
@IBOutlet private var highlightedBackgroundView: UIView!

private var action: AlertAction?
private var textColor: UIColor?

var isEnabled = true {
didSet { self.titleLabel.isEnabled = self.isEnabled }
}
Expand All @@ -16,6 +18,7 @@ final class ActionCell: UICollectionViewCell {
}

func set(_ action: AlertAction, with visualStyle: AlertVisualStyle) {
self.action = action
action.actionView = self

self.titleLabel.font = visualStyle.font(for: action)
Expand All @@ -27,13 +30,47 @@ final class ActionCell: UICollectionViewCell {

self.highlightedBackgroundView.backgroundColor = visualStyle.actionHighlightColor

if let imageView = action.imageView {
stackView.insertArrangedSubview(imageView, at: 0)
}
if let accessoryView = action.accessoryView {
stackView.addArrangedSubview(accessoryView)
}
titleLabel.textAlignment = visualStyle.textAlignment(for: action)
self.setupAccessibility(using: action)
}

override func tintColorDidChange() {
super.tintColorDidChange()
self.titleLabel.textColor = self.textColor ?? self.tintColor
}

override func layoutSubviews() {
super.layoutSubviews()

if let imageView = action?.imageView {
constrainSecondaryView(imageView)
}
if let accessoryView = action?.accessoryView {
constrainSecondaryView(accessoryView)
}
}

private func constrainSecondaryView(_ view: UIView) {
var size = view.intrinsicContentSize
if size.height == UIView.noIntrinsicMetric || size.width == UIView.noIntrinsicMetric {
size = CGSize(width: stackView.bounds.height, height: stackView.bounds.height)
}
if size.height > stackView.bounds.height {
// if size doesn't fit, scale proportionally
size.width /= size.height / stackView.bounds.height
size.height = stackView.bounds.height
}
NSLayoutConstraint.deactivate(view.constraints)
view.heightAnchor.constraint(equalToConstant: size.height).isActive = true
view.widthAnchor.constraint(equalToConstant: size.width).isActive = true
view.setContentHuggingPriority(UILayoutPriority(rawValue: 800), for: .horizontal) // must be higher than 760 for UIStackView to accept it. Ensures to maximize space for titleLabel.
}
}

final class ActionSeparatorView: UICollectionReusableView {
Expand Down
Loading