Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/basthomas/imagealertaction
Image support for UIAlertAction
https://github.com/basthomas/imagealertaction
image swift uialertaction uialertcontroller uiimage uikit
Last synced: about 2 months ago
JSON representation
Image support for UIAlertAction
- Host: GitHub
- URL: https://github.com/basthomas/imagealertaction
- Owner: BasThomas
- License: mit
- Created: 2018-07-09T21:27:38.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2023-03-17T05:45:13.000Z (almost 2 years ago)
- Last Synced: 2024-10-11T11:04:17.586Z (3 months ago)
- Topics: image, swift, uialertaction, uialertcontroller, uiimage, uikit
- Language: Swift
- Size: 223 KB
- Stars: 34
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![Travis status](https://img.shields.io/travis/BasThomas/ImageAlertAction.svg)](https://travis-ci.org/BasThomas/ImageAlertAction)
# ImageAlertAction
`ImageAlertAction` is a `UIAlertAction` extension that adds support for an image
in the action's button.
## Example
To run the example project, clone the repository, and run `pod install` from the Example
directory first.## Installation
ImageAlertAction is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'ImageAlertAction'
```## Usage
### Adding an image to a `UIAlertAction`
Create a `UIAlertAction` like you'd do normally, and pass an image to the `image` parameter.
This will add the image on the left of the action's button.```swift
let settings = UIAlertAction(
title: "Settings",
image: #imageLiteral(resourceName: "settings"),
style: .default
)
```#### Keeping the `UIImage`'s original color
By default, the image provided will be treated as a template, and will be recolored based on the
action's `style`. If you want to draw the original image, you can pass an image with an
explicit rendering mode.```swift
let settingsImage = #imageLiteral(resourceName: "settings").withRenderingMode(.alwaysOriginal)
let settings = UIAlertAction(
title: "Settings",
image: settingsImage,
style: .default
)
```#### Accessing the added `UIImage`
As with the title and style, you can access the image set on the `UIAlertAction`.
```swift
let settings = UIAlertAction(
title: "Settings",
image: #imageLiteral(resourceName: "settings"),
style: .default
)
settings.image // returns an optional UIImage
```#### Adding a checkmark
You can also show a check mark on actions via `isChecked`.
```swift
let settings = UIAlertAction(
title: "Settings",
image: #imageLiteral(resourceName: "settings"),
isChecked: true
style: .default
)
settings.isChecked // returns a Bool
```### Presenting the `UIAlertController`
To present a `UIAlertController` containing the `UIAlertAction`, nothing changes.
```swift
let alertController = UIAlertController(
title: "Title",
message: "Message",
preferredStyle: .actionSheet
)let settings = UIAlertAction(
title: "Settings",
image: #imageLiteral(resourceName: "settings"),
style: .default
)
alertController.addAction(settings)present(alertController, animated: true)
```## Acknowledgements
- Created by [Bas Broek](https://twitter.com/basthomas)
## License
ImageAlertAction is available under the MIT license. See the [LICENSE](LICENSE) file for more
info.