Skip to content

Commit

Permalink
1. CFAlertViewController class made open so that it devs can subclass
Browse files Browse the repository at this point in the history
2. Init method support added
  • Loading branch information
Shardul Patel authored and Shardul Patel committed Jun 9, 2017
1 parent e18aea6 commit 80dc20b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 45 deletions.
108 changes: 63 additions & 45 deletions CFAlertViewController/CFAlertViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit


@objc(CFAlertViewController)
public class CFAlertViewController: UIViewController {
open class CFAlertViewController: UIViewController {

// MARK: - Declarations
public typealias CFAlertViewControllerDismissBlock = (_ isBackgroundTapped: Bool) -> ()
Expand All @@ -24,13 +24,13 @@ public class CFAlertViewController: UIViewController {
case plain = 0
case blur
}
public static func CF_ALERT_DEFAULT_BACKGROUND_COLOR() -> UIColor {
open static func CF_ALERT_DEFAULT_BACKGROUND_COLOR() -> UIColor {
return UIColor(white: 0.0, alpha: 0.7)
}
public static func CF_ALERT_DEFAULT_TITLE_COLOR() -> UIColor {
open static func CF_ALERT_DEFAULT_TITLE_COLOR() -> UIColor {
return UIColor.init(red: 1.0/255.0, green: 51.0/255.0, blue: 86.0/255.0, alpha: 1.0)
}
public static func CF_ALERT_DEFAULT_MESSAGE_COLOR() -> UIColor {
open static func CF_ALERT_DEFAULT_MESSAGE_COLOR() -> UIColor {
return UIColor.init(red: 1.0/255.0, green: 51.0/255.0, blue: 86.0/255.0, alpha: 1.0)
}

Expand Down Expand Up @@ -155,25 +155,6 @@ public class CFAlertViewController: UIViewController {
didDismissAlertHandler: dismiss)
}

public class func alertController(title: String?,
message: String?,
textAlignment: NSTextAlignment,
preferredStyle: CFAlertControllerStyle,
headerView: UIView?,
footerView: UIView?,
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) -> CFAlertViewController {

return CFAlertViewController.alertController(title: title,
titleColor: nil,
message: message,
messageColor: nil,
textAlignment: textAlignment,
preferredStyle: preferredStyle,
headerView: headerView,
footerView: footerView,
didDismissAlertHandler: dismiss)
}

public class func alertController(title: String?,
titleColor: UIColor?,
message: String?,
Expand All @@ -184,47 +165,84 @@ public class CFAlertViewController: UIViewController {
footerView: UIView?,
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) -> CFAlertViewController {

// Create New Instance Of Alert Controller
return CFAlertViewController.init(title: title,
titleColor: titleColor,
message: message,
messageColor: messageColor,
textAlignment: textAlignment,
preferredStyle: preferredStyle,
headerView: headerView,
footerView: footerView,
didDismissAlertHandler: dismiss)
}

public convenience init(title: String?,
message: String?,
textAlignment: NSTextAlignment,
preferredStyle: CFAlertControllerStyle,
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) {

// Create New Instance Of Alert Controller
self.init(title: title,
titleColor: nil,
message: message,
messageColor: nil,
textAlignment: textAlignment,
preferredStyle: preferredStyle,
headerView: nil,
footerView: nil,
didDismissAlertHandler: dismiss)
}

public convenience init(title: String?,
titleColor: UIColor?,
message: String?,
messageColor: UIColor?,
textAlignment: NSTextAlignment,
preferredStyle: CFAlertControllerStyle,
headerView: UIView?,
footerView: UIView?,
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) {

// Get Current Bundle
let bundle = Bundle(for: CFAlertViewController.self)

// Create New Instance Of Alert Controller
let alert = CFAlertViewController.init(nibName: "CFAlertViewController", bundle: bundle)
self.init(nibName: "CFAlertViewController", bundle: bundle)

// Assign Properties
alert.preferredStyle = preferredStyle
alert.backgroundStyle = .plain
alert.backgroundColor = CF_ALERT_DEFAULT_BACKGROUND_COLOR()
alert.titleString = title
self.preferredStyle = preferredStyle
backgroundStyle = .plain
backgroundColor = CFAlertViewController.CF_ALERT_DEFAULT_BACKGROUND_COLOR()
titleString = title
if let titleColor = titleColor {
alert.titleColor = titleColor
self.titleColor = titleColor
}

alert.messageString = message
messageString = message
if let messageColor = messageColor {
alert.messageColor = messageColor
self.messageColor = messageColor
}

alert.textAlignment = textAlignment
alert.setHeaderView(headerView, shouldUpdateContainerFrame: false, withAnimation: false)
alert.setFooterView(footerView, shouldUpdateContainerFrame: false, withAnimation: false)
alert.dismissHandler = dismiss
self.textAlignment = textAlignment
setHeaderView(headerView, shouldUpdateContainerFrame: false, withAnimation: false)
setFooterView(footerView, shouldUpdateContainerFrame: false, withAnimation: false)
dismissHandler = dismiss

// Custom Presentation
alert.modalPresentationStyle = .custom
alert.transitioningDelegate = alert
modalPresentationStyle = .custom
transitioningDelegate = self

// Preload View
if #available(iOS 9.0, *) {
alert.loadViewIfNeeded()
loadViewIfNeeded()
} else {
// Fallback on earlier versions
}

return alert
}



// MARK: - View Life Cycle Methods
internal func loadVariables() {

Expand Down Expand Up @@ -264,7 +282,7 @@ public class CFAlertViewController: UIViewController {
view.addGestureRecognizer(self.tapGesture)
}

public override func viewDidLoad() {
open override func viewDidLoad() {
super.viewDidLoad()

// Load Variables
Expand All @@ -274,7 +292,7 @@ public class CFAlertViewController: UIViewController {
loadDisplayContent()
}

public override func viewWillAppear(_ animated: Bool) {
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// Update UI
Expand Down Expand Up @@ -481,7 +499,7 @@ public class CFAlertViewController: UIViewController {


// MARK: - View Rotation / Size Change Method
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// Code here will execute before the rotation begins.
// Equivalent to placing it in the deprecated method -[willRotateToInterfaceOrientation:duration:]
Expand All @@ -498,7 +516,7 @@ public class CFAlertViewController: UIViewController {


// MARK: - Key Value Observers
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == "contentSize") {
// Update Container View Frame Without Animation
updateContainerViewFrame(withAnimation: false)
Expand Down
Binary file not shown.

0 comments on commit 80dc20b

Please sign in to comment.