WindowAlert 3.0.0

WindowAlert 3.0.0

TestsTested
LangLanguage SwiftSwift
License NOASSERTION
ReleasedLast Release Feb 2020
SPMSupports SPM

Maintained by Alexander Leontev.



  • By
  • Alexander Leontev

WindowAlert

Version License Platform Swift Version Swift Version

  • Hate having providing UIViewController every time you need to show a dialog?
  • Have a stack of modal controllers, and figuring out which one is currently visible makes you groan?
  • UIAlertView.show() makes you feel nostalgic?

This library is a solution for all of these things!

While introducing unified sheet and dialog controller was a great decision, making it a presentable view controller was a shoot and miss on Apple side (Android Fragment dialogs, hi!).

To achieve simplicity of UIAlertView, this library creates a separate window on top of everything with transparent root view controller, and presents your alert controller in said window. You just need to set up WindowAlert, and then showing your dialog is as simple as calling show() on your alert instance!

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Swift 4.0, 4.2 or 5.0 must be used in the project.

Installation

WindowAlert is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'WindowAlert', '~> 3.0.0'

Usage

The usage is quite simple and for most parts it mimicks UIAlertController APIs.

The following piece of code will create alert dialog with one text field, title, message and one action:

let alert = WindowAlert(title: "This is a title", message: "This is a message", preferredStyle: .alert)

alert.addTextField { textField in
  textField.text = "I'm a text field inside WindowAlert!"
}

alert.add(action: WindowAlertAction(
                title: "Got it!",
                style: .default))

alert.show()

This code can be called anywhere - no need to pass UIViewControllers to present it on.

Additional bonus features

  • Set alignment of alert action:
WindowAlertAction(
                title: "Got it!",
                style: .default,
                titleAlignment: .left)
  • Add image to alert action:
WindowAlertAction(
                title: "Got it!",
                style: .default,
                image: UIImage(named: "someImage"))
  • Dismiss alert on tap outside:
let alert = WindowAlert(title: "This is a title", message: "This is a message", preferredStyle: .alert)
alert.hideOnTapOutside = true

Warning: images and title alignment are based on Apple internal implementation, and they may break without notice after iOS update. If you find it broke after an update, please let me know and I will issue an update ASAP.

Author

Alexander Leontev, [email protected]

License

WindowAlert is available under the Apache 2.0 license. See the LICENSE file for more info.