-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathActionSheet.swift
115 lines (96 loc) · 4.53 KB
/
ActionSheet.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// ActionSheet.swift
// Alertift
//
// Created by Suguru Kishimoto on 4/27/17.
// Copyright © 2017 Suguru Kishimoto. All rights reserved.
//
import Foundation
import UIKit
extension Alertift {
/// ActionSheet
final public class ActionSheet: AlertType, _AlertType {
public typealias Handler = (UIAlertAction, Int) -> Void
var _alertController: InnerAlertController!
public var alertController: UIAlertController {
return _alertController as UIAlertController
}
public static var backgroundColor: UIColor?
public static var buttonTextColor: UIColor?
public static var titleTextColor: UIColor?
public static var messageTextColor: UIColor?
/// Make action sheet
///
/// - Parameters:
/// - title: The title of the alert. Use this string to get the user’s attention and communicate the reason for the alert.
/// - message: Descriptive text that provides additional details about the reason for the alert.
/// - Returns: Instance of **ActionSheet**
public init(title: String? = nil, message: String? = nil) {
buildAlertControlelr(title: title, message: message, style: .actionSheet)
}
/// Add action to alertController
public func action(_ action: Alertift.Action, handler: Handler?) -> Self {
return self.action(action, image: nil, handler: handler)
}
public func action(_ action: Alertift.Action, handler: ShortHandler? = nil) -> Self {
return self.action(action) { _, _ in handler?() }
}
/// Add action to alertController
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImage.RenderingMode = .automatic, handler: Handler?) -> Self {
let alertAction = buildAlertAction(action, handler:
merge(_alertController.actionHandler, handler ?? { (_, _) in })
)
if let image = image {
alertAction.setValue(image.withRenderingMode(renderingMode), forKey: "image")
}
_alertController.addAction(alertAction)
return self
}
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImage.RenderingMode = .automatic, handler: ShortHandler? = nil) -> Self {
return self.action(action, image: image, renderingMode: renderingMode) { _, _ in handler?() }
}
/// Add sourceView and sourceRect to **popoverPresentationController**.
///
/// If you want to use action sheet on iPad, you have to use this method.
/// - Parameters:
/// - view: sourceView
/// - rect: sourceRect
/// - Returns: Myself
public func popover(sourceView view: UIView?, sourceRect rect: CGRect) -> Self {
_alertController.popoverPresentationController?.sourceView = view
_alertController.popoverPresentationController?.sourceRect = rect
return self
}
/// Add sourceView and sourceRect to **popoverPresentationController** using anchorView.
///
/// If you want to use action sheet on iPad, you have to use this method.
/// - Parameters:
/// - anchorView: will be anchor of popoverPresentationController.
/// - Returns: Myself
public func popover(anchorView: UIView) -> Self {
_alertController.popoverPresentationController?.sourceView = anchorView.superview
_alertController.popoverPresentationController?.sourceRect = anchorView.frame
return self
}
/// Add barButtonItem to **popoverPresentationController**.
///
/// If you want to use action sheet on iPad, you have to use this method.
/// - Parameters:
/// - barButtonItem: UIBarButtonItem
/// - Returns: Myself
public func popover(barButtonItem: UIBarButtonItem?) -> Self {
_alertController.popoverPresentationController?.barButtonItem = barButtonItem
return self
}
func convertFinallyHandler(_ handler: Any) -> InnerAlertController.FinallyHandler {
return { (action, index, _) in (handler as? Handler)?(action, index) }
}
public func image(_ image: UIImage?, imageTopMargin: Alertift.ImageTopMargin = .none) -> Self {
_alertController.setImage(image, imageTopMargin: imageTopMargin)
return self
}
deinit {
Debug.log()
}
}
}