Skip to content

Instantly share code, notes, and snippets.

@calvingit
Created November 4, 2023 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvingit/537b719fcc1f44a074ab12ed1fc14bb7 to your computer and use it in GitHub Desktop.
Save calvingit/537b719fcc1f44a074ab12ed1fc14bb7 to your computer and use it in GitHub Desktop.
import UIKit
// MARK: - Initializers
extension UIAlertController {
/// Create new alert view controller.
///
/// - Parameters:
/// - style: alert controller's style.
/// - title: alert controller's title.
/// - message: alert controller's message (default is nil).
/// - defaultActionButtonTitle: default action button title (default is "OK")
/// - tintColor: alert controller's tint color (default is nil)
convenience init(style: UIAlertController.Style, source: UIView? = nil, title: String? = nil, message: String? = nil, tintColor: UIColor? = nil) {
self.init(title: title, message: message, preferredStyle: style)
// TODO: for iPad or other views
let isPad: Bool = UIDevice.current.userInterfaceIdiom == .pad
let root = UIApplication.shared.keyWindow?.rootViewController?.view
//self.responds(to: #selector(getter: popoverPresentationController))
if let source = source {
popoverPresentationController?.sourceView = source
popoverPresentationController?.sourceRect = source.bounds
} else if isPad, let source = root, style == .actionSheet {
popoverPresentationController?.sourceView = source
popoverPresentationController?.sourceRect = CGRect(x: source.bounds.midX, y: source.bounds.midY, width: 0, height: 0)
//popoverPresentationController?.permittedArrowDirections = .down
popoverPresentationController?.permittedArrowDirections = .init(rawValue: 0)
}
if let color = tintColor {
self.view.tintColor = color
}
}
}
// MARK: - Methods
extension UIAlertController {
/// Add an action to Alert
///
/// - Parameters:
/// - title: action title
/// - style: action style (default is UIAlertActionStyle.default)
/// - isEnabled: isEnabled status for action (default is true)
/// - handler: optional action handler to be called when button is tapped (default is nil)
func addAction(image: UIImage? = nil, title: String, color: UIColor? = nil, backgroundColor: UIColor? = nil, style: UIAlertAction.Style = .default, isEnabled: Bool = true, handler: ((UIAlertAction) -> Void)? = nil) {
//let isPad: Bool = UIDevice.current.userInterfaceIdiom == .pad
//let action = UIAlertAction(title: title, style: isPad && style == .cancel ? .default : style, handler: handler)
let action = UIAlertAction(title: title, style: style, handler: handler)
action.isEnabled = isEnabled
// button image
if let image = image {
action.setValue(image, forKey: "image")
}
// button title color
if let color = color {
action.setValue(color, forKey: "titleTextColor")
}
// button title color
if let backgroundColor = color {
action.setValue(UIColor.red, forKey: "backgroundColor")
}
addAction(action)
}
/// Set alert's title, font and color
///
/// - Parameters:
/// - title: alert title
/// - font: alert title font
/// - color: alert title color
func set(title: String?, font: UIFont, color: UIColor) {
if title != nil {
self.title = title
}
setTitle(font: font, color: color)
}
func setTitle(font: UIFont, color: UIColor) {
guard let title = self.title else { return }
let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: color]
let attributedTitle = NSMutableAttributedString(string: title, attributes: attributes)
setValue(attributedTitle, forKey: "attributedTitle")
}
/// Set alert's message, font and color
///
/// - Parameters:
/// - message: alert message
/// - font: alert message font
/// - color: alert message color
func set(message: String?, font: UIFont, color: UIColor) {
if message != nil {
self.message = message
}
setMessage(font: font, color: color)
}
func setMessage(font: UIFont, color: UIColor) {
guard let message = self.message else { return }
let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: color]
let attributedMessage = NSMutableAttributedString(string: message, attributes: attributes)
setValue(attributedMessage, forKey: "attributedMessage")
}
/// Set alert's content viewController
///
/// - Parameters:
/// - vc: ViewController
/// - height: height of content viewController
func set(vc: UIViewController?, width: CGFloat? = nil, height: CGFloat? = nil) {
guard let vc = vc else { return }
setValue(vc, forKey: "contentViewController")
if let height = height {
vc.preferredContentSize.height = height
preferredContentSize.height = height
}
}
}
@objc
extension UIViewController {
@objc
func showAlert(_ content: UIViewController) {
let alert = UIAlertController(style: .alert)
alert.set(vc: content)
present(alert, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment