Swiftpack.co - Package - sberrevoets/SDCAlertView

SDCAlertView

SDCAlertView started out as an alert that looked identical to UIAlertView, but had support for a custom content view. With the introduction of UIAlertController in iOS 8, the project was updated to the more modern API that UIAlertController brought.

Features

  • ☑ Most UIAlertController functionality
  • ☑ Custom content views
  • ☑ Preventing controllers from dismissing when the user taps a button
  • ☑ Easy presentation/dismissal
  • ☑ Attributed title label, message label, and buttons
  • ☑ Appearance customization
  • ☑ Usable from Swift and Objective-C
  • ☑ Understandable button placement
  • ☑ UI tests
  • ☑ Custom alert behavior
  • ☑ CocoaPods/Carthage/Swift Package Manager support
  • ☐ Easy queueing of alerts

Requirements

  • Swift 5.0
  • iOS 9 or higher

Installation

CocoaPods

To install SDCAlertView using CocoaPods, integrate it in your existing Podfile, or create a new Podfile:

platform :ios, '9.0'
use_frameworks!

target 'MyApp' do
  pod 'SDCAlertView'
end

Then run pod install.

Carthage

To install with Carthage, add the following line to your Cartfile:

github "sberrevoets/SDCAlertView"

Run carthage update and drag SDCAlertView.framework in the Build folder into your project.

Swift Package Manager

SPM does not yet support iOS, but SDCAlertView will be available there once it does.

Alerts vs. Action Sheets

SDCAlertController supports the presentation of action sheets, but there are some limitations and things to keep in mind when using action sheets:

  • It does not properly adapt on iPad. This is because iOS doesn't support UIModalPresentationStyle.Custom for adaptive presentations (such as when presenting an action sheet from a bar button item).
  • The new AlertBehaviors is, due to limitations in the Swift/Objective-C interop, not available when using SDCAlertController from Swift. This affects AlertControllerStyle.Alert as well.
  • When adding subviews to the custom content view, that view will replace the title and message labels.

Usage

SDCAlertView is written in Swift, but can be used in both Swift and Objective-C. Corresponding types in Objective-C have the same name they do in Swift, but with an SDC prefix.

Basic

let alert = AlertController(title: "Title", message: "This is a message", preferredStyle: .alert)
alert.addAction(AlertAction(title: "Cancel", style: .normal))
alert.addAction(AlertAction(title: "OK", style: .preferred))
alert.present()

// or use the convenience methods:

AlertController.alert(withTitle: "Title", message: "This is a message", actionTitle: "OK")
AlertController.sheet(withTitle: "Action sheet title", "Action sheet message", actions: ["OK", "Cancel"])

Custom Content Views

let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
spinner.translatesAutoresizingMaskIntoConstraints = false
spinner.startAnimating()

let alert = AlertController(title: "Title", message: "Please wait...")
alert.contentView.addSubview(spinner)

spinner.centerXAnchor.constraint(equalTo: alert.contentView.centerXAnchor).isActive = true
spinner.topAnchor.constraint(equalTo: alert.contentView.topAnchor).isActive = true
spinner.bottomAnchor.constraint(equalTo: alert.contentView.bottomAnchor).isActive = true

alert.present()

Dismissal Prevention

let alert = AlertController(title: "Title", message: "This is a message")
alert.addAction(AlertAction(title: "Dismiss", style: .preferred))
alert.addAction(AlertAction(title: "Don't dismiss", style: .normal))
alert.shouldDismissHandler = { $0.title == "Dismiss" }
alert.present()

Styling and Appearance

SDCAlertController is a normal view controller, so applying a tintColor to its view will color the buttons and any subviews you add to the contentView.

If you are looking for more customizations, create a subclass of AlertVisualStyle and use visualStyle on the AlertController instance. You can also create an instance of AlertVisualStyle and overwrite the attributes you need (this is mainly intended to be used from Objective-C). Note that after an alert has been presented, changing any of these settings is ignored.

License

SDCAlertView is distributed under the MIT license.

Github

link
Stars: 1670

Dependencies

Used By

Total: 0

Releases

11.1.1 - 2020-04-26 23:19:13

11.1.1

What's New:

  • Refactored the action sheet UI to code, eliminating the hard-to-understand XIB

11.1 - 2020-04-26 23:13:10

11.1

What's New:

  • Action sheets can now receive an image view on the left and accessory view on the right of each action
  • The color of the chrome/dimming view can now be set on visual styles
  • The spacing between title/message labels has been updated to the ones UIAlertController uses

Bug Fixes:

  • View controllers presented from alerts/action sheets aren't dismissed anymore when using the alert's dismiss() method
  • The area around text fields is now the same background color as alerts when one is set
  • Dark mode appearances are more like the native UIAlertController ones

11.0 - 2020-02-17 23:26:19

11.0

What's New:

  • Dark Mode support on iOS 13 (#298)

10.0 - 2019-04-02 06:41:54

This release brings Swift 5.0 compatibility

9.0.1 - 2018-09-27 04:23:06

Bug Fixes:

  • Fixes incorrect margins for new iPhones

9.0 - 2018-09-24 06:38:54

This release brings Swift 4.2 compatibility.

8.1.1 - 2018-09-02 04:16:08

Bug Fixes:

  • Fixes incorrect spacing when an action sheet doesn't have a label

8.1 - 2018-05-22 15:30:57

This release brings Swift 4.1 compatibility.

Bug Fixes:

  • Fixes build issues when using the new build system and CocoaPods
  • Fixes a missing contentView in action sheets

8.0.2 - 2018-01-17 02:09:43

Bug Fixes:

  • Fixes the inverted dismissOnOutsideTap behavior for alert views
  • Hides action sheet labels when no title or subtitle are provided
  • Fixes action sheet layout issues on iPhone X
  • Invokes the preferredAction when the return key is hit on a hardware keyboard
  • Return no preferredAction when the style of the alert is an action sheet

8.0.1 - 2017-09-24 06:46:45

Bug Fixes:

  • Fixes a layout issue when creating an alert with a custom content view

8.0 - 2017-09-22 08:26:23

8.0

This release brings Swift 4 and iOS 11 compatibility. It also increases the deployment target to iOS 9.0.

What's New:

  • Adds a closure for handling taps in the outer (chrome) area of the alert
  • Improves accessibility for alert actions
  • AlertController.add() has been renamed to AlertController.addAction() for clarity
  • The AlertBehaviors constants are now lowercased, following Swift 3 conventions.

Bug Fixes:

  • Fixes an issue that could lead the alert to be shown in an unsupported orientation

7.1.2 - 2017-03-25 21:02:34

Bug Fixes:

  • Fixes a bug that could incorrectly set cancel button attributes on action sheets

7.1.1 - 2017-02-27 05:30:48

Bug fixes:

  • Fixes a retain cycle when adding text fields to the alert
  • Properly makes the first text field the first responder when presenting an alert with text fields

7.1 - 2016-11-01 06:54:24

What's New:

  • Makes AlertVisualStyle subclassable again
  • Adds support for custom background colors in action sheets
  • Adds Taptic feedback when dragging between buttons on iPhone 7

Bug Fixes:

  • The dismissal animation looks like the system one again
  • Action sheets without an explicit cancel button won't show the inferred cancel button twice anymore
  • Button labels size and truncate as expected now, instead of being cut off

7.0.1 - 2016-09-15 18:50:42

Bug Fixes:

  • Avoids an infinite loop/crash when using an action sheet without explicit cancel buttons

7.0 (Swift 3) - 2016-09-14 18:28:13

This is a compatibility update for Swift 3.

Bug Fixes:

  • Correctly calls the cancel button's handler in action sheets (#161)

6.0 (Swift 2.3) - 2016-09-12 21:01:32

This is a compatibility update for Swift 2.3.

5.1.1 - 2016-07-31 08:00:13

Bug Fixes:

  • Gives action buttons the button trait for Voice Over

5.1 - 2016-05-19 05:50:14

Bug Fixes:

  • Fixes the inability to override visual style properties in a subclass of DefaultVisualStyle. The VisualStyle protocol has been removed and the conforming class been renamed to AlertVisualStyle. The old class name is still available, but marked as deprecated and will be removed in the future.

What's New:

  • Makes an AlertAction's handler public.

5.0 - 2016-03-24 04:43:55

5.0 is a compatibility update so the project builds in Swift 2.2 and doesn't generate warnings. It also changes the Objective-C names of the public enums, which Swift now supports.

Bug Fixes:

  • Prioritize textColorForAction() over the tintColor of an action
  • Properly exposes visualStyle as a property on SDCAlertController in Objective-C
  • Makes actionLayout a non-optional, allowing it to be exposed to Objective-C
  • Exposes a public init method in DefaultVisualStyle so subclassers don't have to implement this separately
  • Correctly shows buttons if an alert has scrollable content after rotation
  • Fixes incorrect accessibility labels on buttons

4.0.1 - 2015-12-29 15:35:19

Bug Fixes:

  • Fixes incorrect fonts for text in alerts

4.0 - 2015-12-27 19:44:15

What's New:

  • Adds support for presenting action sheets
  • Implements alert behaviors such as parallax and "tap outside to dismiss"
  • Action highlight colors can be changed with custom visual styles

Changes: This version introduces other changes that are not compatible with previous versions of SDCAlertView.

  • The automaticallyFocusFirstTextField property is now implemented as an alert behavior
  • In Objective-C, the presentation and dismissal methods are now named presentAnimated:completion: and dismissAnimated:completion: to follow Objective-C nomenclature more closely
  • The title and message label fonts are removed from VisualStyle. To change the labels' fonts, please use attributedTitle and attributedMessage with NSFontAttributeName instead.
  • setShouldDismissHandler() and setVisualStyle() are now properties named shouldDismissHandler and visualStyle respectively. Their functionality is unchanged.
  • The convenience method showWithTitle(_:message:actionTitle:customView:) has been renamed to alertWithTitle(_:message:actionTitle:customView:) to provide more consistency with the action sheet's counterpart

3.1.1 - 2015-12-17 16:06:05

Bug Fixes:

  • Fixes a retain cycle
  • Resolves an issue that would not correctly disable actions when needed

3.1 - 2015-12-15 05:14:52

What's New:

  • Adds an option to give the alert a different background color

Bug Fixes:

  • Fixes a bug that would not apply appropriate padding to the labels

3.0.4 - 2015-11-23 05:56:44

Bug Fixes:

  • Fixes a crash on iOS 8 when adding text fields to the alert

3.0.3 - 2015-11-02 04:47:51

Bug Fixes:

  • Resolves an issue that would never use the white status bar color if it was specified

3.0.2 - 2015-10-25 06:10:34

Bug Fixes:

  • Fixes a major issue that would simply not display an alert
  • The example project now formally depends on the SDCAlertView target
  • Added a missing docstring

3.0.1 - 2015-10-11 04:58:32

Bug Fixes:

  • Fixes an issue that would sometimes use the wrong scroll direction for actions
  • Fixes the pod spec so it refers to the correct tag, not a branch
  • Removes some unused (overridden) methods in the Objective-C bridging header

3.0 - 2015-10-07 07:15:03

What's New:

  • Completely rewritten in Swift
  • Updated for iOS 9
  • Deployment target increased to iOS 8

2.5.4 - 2015-09-25 03:25:24

Bug Fixes:

  • Fixes a bug where long action titles could extend outside the bounds of their containing button