Skip to content

Commit

Permalink
Improved actionsheet's APIs about popover
Browse files Browse the repository at this point in the history
  • Loading branch information
sgr-ksmt committed May 6, 2017
1 parent 251e459 commit 93beae7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ViewController: UIViewController {
case .login:
showLoginAlert()
case .actionsheet:
showActionSheet(source: self.view, frame: button.frame)
showActionSheet(anchorView: button)
}
}

Expand Down Expand Up @@ -101,9 +101,8 @@ class ViewController: UIViewController {
})
.show()
}
private func showActionSheet(source: UIView, frame: CGRect) {
Alertift.actionSheet(message: "Which food do you like?")
.popover(sourceView: source, sourceRect: frame)
private func showActionSheet(anchorView: UIView) {
Alertift.actionSheet(message: "Which food do you like?", anchorView: anchorView)
.action(.default("🍣"))
.action(.default("🍎"))
.action(.default("🍖"))
Expand Down
7 changes: 7 additions & 0 deletions Sources/ActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ extension Alertift {
_alertController.popoverPresentationController?.sourceRect = rect
return self
}

public func popover(anchorView: UIView) -> Self {
_alertController.popoverPresentationController?.sourceView = anchorView.superview
_alertController.popoverPresentationController?.sourceRect = anchorView.frame
return self
}


/// Add barButtonItem to **popoverPresentationController**.
///
Expand Down
5 changes: 5 additions & 0 deletions Sources/AlertType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ extension AlertType {
/// - viewController: The view controller to display over the current view controller’s content. Default is **UIApplication.shared.keyWindow?.rootViewController**
/// - completion: The block to execute after the presentation finishes. This block has no return value and takes no parameters. You may specify nil for this parameter.
final public func show(on viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController, completion: (() -> Void)? = nil) {
if _alertController.preferredStyle == .actionSheet && UIScreen.main.traitCollection.userInterfaceIdiom == .pad {
if _alertController.popoverPresentationController?.sourceView == nil {
_alertController.popoverPresentationController?.sourceView = viewController?.view
}
}
viewController?.present(_alertController, animated: true, completion: completion)
}
}
4 changes: 4 additions & 0 deletions Sources/Alertift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public final class Alertift {
public static func actionSheet(title: String? = nil, message: String? = nil) -> ActionSheet {
return ActionSheet(title: title, message: message)
}

public static func actionSheet(title: String? = nil, message: String? = nil, anchorView: UIView) -> ActionSheet {
return ActionSheet(title: title, message: message).popover(anchorView: anchorView)
}
}

extension Alertift {
Expand Down

0 comments on commit 93beae7

Please sign in to comment.