Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

The little alert that could

License

NotificationsYou must be signed in to change notification settings

sberrevoets/SDCAlertView

Repository files navigation

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

Features

  • MostUIAlertController 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'dopod'SDCAlertView'end

Then runpod install.

Carthage

To install with Carthage, add the following line to yourCartfile:

github"sberrevoets/SDCAlertView"

Runcarthage update and dragSDCAlertView.framework in theBuild folder into your project.

Swift Package Manager

To install with Swift Package Manager, add this package to your project’s 'Swift Packages' section. Or add the following line to yourPackage.swift:

.package(url:"https://github.com/sberrevoets/SDCAlertView.git", from:"12.0.5")

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 supportUIModalPresentationStyle.Custom for adaptive presentations (such as when presenting an action sheet from a bar button item).
  • The newAlertBehaviors is, due to limitations in the Swift/Objective-C interop, not available when usingSDCAlertController from Swift. This affectsAlertControllerStyle.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 anSDC prefix.

Basic

letalert=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

letspinner=UIActivityIndicatorView(activityIndicatorStyle:.gray)spinner.translatesAutoresizingMaskIntoConstraints=falsespinner.startAnimating()letalert=AlertController(title:"Title", message:"Please wait...")alert.contentView.addSubview(spinner)spinner.centerXAnchor.constraint(equalTo: alert.contentView.centerXAnchor).isActive=truespinner.topAnchor.constraint(equalTo: alert.contentView.topAnchor).isActive=truespinner.bottomAnchor.constraint(equalTo: alert.contentView.bottomAnchor).isActive=truealert.present()

Dismissal Prevention

letalert=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 atintColor to itsview will color the buttons and any subviews you add to thecontentView.

If you are looking for more customizations, create a subclass ofAlertVisualStyle and usevisualStyle on theAlertController instance. You can also create an instance ofAlertVisualStyle 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.


[8]ページ先頭

©2009-2025 Movatter.jp