Giter Site home page Giter Site logo

sdcalertview's Introduction

SDCAlertView

CI Status CocoaPods Carthage

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 3
  • iOS 8 or higher

If you want to use the library on iOS 7, please use version 2.5.4 (the latest 2.x release).

Installation

CocoaPods

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

platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
  pod 'SDCAlertView', '~> 7.1'
end

Then run pod install.

Carthage

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

github "sberrevoets/SDCAlertView" ~> 7.1

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.add(AlertAction(title: "Cancel", style: .default))
alert.add(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.constraintEqualToAnchor(alert.contentView.centerXAnchor).active = true
spinner.topAnchor.constraintEqualToAnchor(alert.contentView.topAnchor).active = true
spinner.bottomAnchor.constraintEqualToAnchor(alert.contentView.bottomAnchor).active = true

alert.present()

Dismissal Prevention

let alert = AlertController(title: "Title", message: "This is a message")
alert.add(AlertAction(title: "Dismiss", style: .preferred))
alert.add(AlertAction(title: "Don't dismiss", style: .default))
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.

Support

I'm pretty active on Stack Overflow, so please use that if you have any questions. If you are experiencing bugs, feel free to post an issue or submit a pull request.

License

SDCAlertView is distributed under the MIT license.

sdcalertview's People

Contributors

amarcadet avatar bcherry avatar castles avatar esilverberg avatar fnxpt avatar franklsf95 avatar fwalex avatar gereons avatar jlubeck avatar justin999 avatar kurry avatar liampronan avatar myster-t avatar nirajignited avatar nschum avatar nzhuk avatar sberrevoets avatar scott90 avatar tizaks avatar twiedow avatar virusman avatar zgl6yxnt avatar zidaneno5 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.