Skip to content

Commit 5e341b5

Browse files
Shardul PatelShardul Patel
authored andcommitted
1. Popup Container View can now be configurable (Color / Corner Raadious) before presenting alert view
2. Alert Title and Message label colors are now configurable
1 parent 85425fb commit 5e341b5

6 files changed

Lines changed: 89 additions & 21 deletions

File tree

CFAlertViewController/CFAlertViewController.swift

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public class CFAlertViewController: UIViewController {
2727
public static func CF_ALERT_DEFAULT_BACKGROUND_COLOR() -> UIColor {
2828
return UIColor(white: 0.0, alpha: 0.7)
2929
}
30+
public static func CF_ALERT_DEFAULT_TITLE_COLOR() -> UIColor {
31+
return UIColor.init(red: 1.0/255.0, green: 51.0/255.0, blue: 86.0/255.0, alpha: 1.0)
32+
}
33+
public static func CF_ALERT_DEFAULT_MESSAGE_COLOR() -> UIColor {
34+
return UIColor.init(red: 1.0/255.0, green: 51.0/255.0, blue: 86.0/255.0, alpha: 1.0)
35+
}
3036

3137
// MARK: - Variables
3238
// MARK: Public
@@ -36,14 +42,10 @@ public class CFAlertViewController: UIViewController {
3642
DispatchQueue.main.async(execute: {
3743
// Position Contraints for Container View
3844
if self.preferredStyle == .actionSheet {
39-
// Set Corner Radius
40-
self.containerView?.layer.cornerRadius = 6.0
4145
self.containerViewCenterYConstraint?.isActive = false
4246
self.containerViewBottomConstraint?.isActive = true
4347
}
4448
else {
45-
// Set Corner Radius
46-
self.containerView?.layer.cornerRadius = 8.0
4749
self.containerViewBottomConstraint?.isActive = false
4850
self.containerViewCenterYConstraint?.isActive = true
4951
}
@@ -104,11 +106,15 @@ public class CFAlertViewController: UIViewController {
104106
@IBOutlet public weak var backgroundBlurView: UIVisualEffectView?
105107
public var shouldDismissOnBackgroundTap: Bool = true // Default is True
106108

107-
@IBOutlet public weak var containerView: UIView? // Reference Container View For Transition
109+
// The view which holds the popup UI
110+
// You can change corner radius or background color of this view for additional customisation
111+
@IBOutlet public weak var containerView: UIView?
108112

109113
// MARK: Private / Internal
110114
internal var titleString: String?
115+
internal var titleColor: UIColor = CFAlertViewController.CF_ALERT_DEFAULT_TITLE_COLOR()
111116
internal var messageString: String?
117+
internal var messageColor: UIColor = CFAlertViewController.CF_ALERT_DEFAULT_MESSAGE_COLOR()
112118
internal var actionList = [CFAlertAction]()
113119
internal var dismissHandler: CFAlertViewControllerDismissBlock?
114120
internal var keyboardHeight: CGFloat = 0.0 {
@@ -131,15 +137,17 @@ public class CFAlertViewController: UIViewController {
131137
@IBOutlet internal weak var tableViewHeightConstraint: NSLayoutConstraint?
132138

133139

134-
// MARK: - Initialisation Method
140+
// MARK: - Initialisation Methods
135141
public class func alertController(title: String?,
136142
message: String?,
137143
textAlignment: NSTextAlignment,
138144
preferredStyle: CFAlertControllerStyle,
139145
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) -> CFAlertViewController {
140146

141147
return CFAlertViewController.alertController(title: title,
148+
titleColor: nil,
142149
message: message,
150+
messageColor: nil,
143151
textAlignment: textAlignment,
144152
preferredStyle: preferredStyle,
145153
headerView: nil,
@@ -155,6 +163,27 @@ public class CFAlertViewController: UIViewController {
155163
footerView: UIView?,
156164
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) -> CFAlertViewController {
157165

166+
return CFAlertViewController.alertController(title: title,
167+
titleColor: nil,
168+
message: message,
169+
messageColor: nil,
170+
textAlignment: textAlignment,
171+
preferredStyle: preferredStyle,
172+
headerView: headerView,
173+
footerView: footerView,
174+
didDismissAlertHandler: dismiss)
175+
}
176+
177+
public class func alertController(title: String?,
178+
titleColor: UIColor?,
179+
message: String?,
180+
messageColor: UIColor?,
181+
textAlignment: NSTextAlignment,
182+
preferredStyle: CFAlertControllerStyle,
183+
headerView: UIView?,
184+
footerView: UIView?,
185+
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) -> CFAlertViewController {
186+
158187
// Get Current Bundle
159188
let bundle = Bundle(for: CFAlertViewController.self)
160189

@@ -166,7 +195,15 @@ public class CFAlertViewController: UIViewController {
166195
alert.backgroundStyle = .plain
167196
alert.backgroundColor = CF_ALERT_DEFAULT_BACKGROUND_COLOR()
168197
alert.titleString = title
198+
if let titleColor = titleColor {
199+
alert.titleColor = titleColor
200+
}
201+
169202
alert.messageString = message
203+
if let messageColor = messageColor {
204+
alert.messageColor = messageColor
205+
}
206+
170207
alert.textAlignment = textAlignment
171208
alert.setHeaderView(headerView, shouldUpdateContainerFrame: false, withAnimation: false)
172209
alert.setFooterView(footerView, shouldUpdateContainerFrame: false, withAnimation: false)
@@ -176,10 +213,18 @@ public class CFAlertViewController: UIViewController {
176213
alert.modalPresentationStyle = .custom
177214
alert.transitioningDelegate = alert
178215

216+
// Preload View
217+
if #available(iOS 9.0, *) {
218+
alert.loadViewIfNeeded()
219+
} else {
220+
// Fallback on earlier versions
221+
}
222+
179223
return alert
180224
}
181225

182226

227+
183228
// MARK: - View Life Cycle Methods
184229
internal func loadVariables() {
185230

@@ -203,6 +248,17 @@ public class CFAlertViewController: UIViewController {
203248

204249
internal func loadDisplayContent() {
205250

251+
// Set Container View Default Background Color
252+
containerView?.backgroundColor = UIColor.white
253+
254+
// Set Container View Default Corner Radius
255+
if preferredStyle == .actionSheet {
256+
containerView?.layer.cornerRadius = 6.0
257+
}
258+
else {
259+
containerView?.layer.cornerRadius = 8.0
260+
}
261+
206262
// Add Tap Gesture Recognizer On View
207263
tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.viewDidTap))
208264
view.addGestureRecognizer(self.tapGesture)
@@ -507,7 +563,7 @@ extension CFAlertViewController: UITableViewDataSource, UITableViewDelegate, CFA
507563
cell = tableView.dequeueReusableCell(withIdentifier: CFAlertTitleSubtitleTableViewCell.identifier())
508564
let titleSubtitleCell: CFAlertTitleSubtitleTableViewCell? = (cell as? CFAlertTitleSubtitleTableViewCell)
509565
// Set Content
510-
titleSubtitleCell?.setTitle(titleString, subtitle: messageString, alignment: textAlignment!)
566+
titleSubtitleCell?.setTitle(titleString, titleColor: titleColor, subtitle: messageString, subtitleColor: messageColor, alignment: textAlignment!)
511567
// Set Content Margin
512568
titleSubtitleCell?.contentTopMargin = 20.0
513569
if self.actionList.count <= 0 {

CFAlertViewController/Cells/CFAlertActionTableViewCell.xib

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="16C67" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
99
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
</dependencies>
@@ -16,7 +16,7 @@
1616
<rect key="frame" x="0.0" y="0.0" width="320" height="50"/>
1717
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
1818
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM">
19-
<rect key="frame" x="0.0" y="0.0" width="320" height="50"/>
19+
<rect key="frame" x="0.0" y="0.0" width="320" height="49.5"/>
2020
<autoresizingMask key="autoresizingMask"/>
2121
<subviews>
2222
<button opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" horizontalHuggingPriority="750" horizontalCompressionResistancePriority="749" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="RuE-Ia-gYW" customClass="CFPushButton">
@@ -31,6 +31,7 @@
3131
</connections>
3232
</button>
3333
</subviews>
34+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
3435
<constraints>
3536
<constraint firstAttribute="bottomMargin" secondItem="RuE-Ia-gYW" secondAttribute="bottom" constant="-8" id="AN7-ji-oSQ"/>
3637
<constraint firstAttribute="trailingMargin" secondItem="RuE-Ia-gYW" secondAttribute="trailing" priority="751" constant="12" id="Jjk-kp-Fb0"/>
@@ -39,6 +40,7 @@
3940
<constraint firstItem="RuE-Ia-gYW" firstAttribute="leading" secondItem="H2p-sc-9uM" secondAttribute="leadingMargin" priority="751" constant="12" id="kDS-GO-jrN"/>
4041
</constraints>
4142
</tableViewCellContentView>
43+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
4244
<connections>
4345
<outlet property="actionButton" destination="RuE-Ia-gYW" id="vvx-z6-Nfn"/>
4446
<outlet property="actionButtonBottomConstraint" destination="AN7-ji-oSQ" id="0fZ-Bt-Hpf"/>

CFAlertViewController/Cells/CFAlertTitleSubtitleTableViewCell.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import UIKit
1111

1212
@objc(CFAlertTitleSubtitleTableViewCell)
1313
public class CFAlertTitleSubtitleTableViewCell: UITableViewCell {
14-
14+
1515
// MARK: - Declarations
1616

1717

@@ -60,11 +60,14 @@ public class CFAlertTitleSubtitleTableViewCell: UITableViewCell {
6060
// MARK: Initialization Methods
6161
public override func awakeFromNib() {
6262
super.awakeFromNib()
63+
64+
// Initialization code
6365
basicInitialisation()
6466
}
6567

6668
public override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
6769
super.init(style: style, reuseIdentifier: reuseIdentifier)
70+
6871
// Initialization code
6972
basicInitialisation()
7073
}
@@ -74,12 +77,13 @@ public class CFAlertTitleSubtitleTableViewCell: UITableViewCell {
7477
}
7578

7679
internal func basicInitialisation() {
77-
// Reset Text
78-
setTitle(nil, subtitle: nil, alignment: .center)
80+
// Reset Text and Color
81+
setTitle(nil, titleColor: nil, subtitle: nil, subtitleColor: nil, alignment: .center)
82+
7983
// Set Content Leading Space
8084
contentLeadingSpace = 20.0;
8185
}
82-
86+
8387

8488
// MARK: - Layout Methods
8589
override public func layoutSubviews() {
@@ -89,12 +93,14 @@ public class CFAlertTitleSubtitleTableViewCell: UITableViewCell {
8993

9094

9195
// MARK: Helper Methods
92-
public func setTitle(_ title: String?, subtitle: String?, alignment: NSTextAlignment) {
96+
public func setTitle(_ title: String?, titleColor: UIColor?, subtitle: String?, subtitleColor: UIColor?, alignment: NSTextAlignment) {
9397

9498
// Set Cell Text Fonts & Colors
9599
titleLabel?.text = title
100+
titleLabel?.textColor = titleColor
96101
titleLabel?.textAlignment = alignment
97102
subtitleLabel?.text = subtitle
103+
subtitleLabel?.textColor = subtitleColor
98104
subtitleLabel?.textAlignment = alignment
99105

100106
// Update Constraints
@@ -116,5 +122,4 @@ public class CFAlertTitleSubtitleTableViewCell: UITableViewCell {
116122
}
117123
}
118124
}
119-
120125
}

CFAlertViewController/Cells/CFAlertTitleSubtitleTableViewCell.xib

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="16C67" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
99
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
</dependencies>
@@ -21,20 +21,20 @@
2121
<subviews>
2222
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Title" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="c2H-hb-4JF">
2323
<rect key="frame" x="20" y="20" width="280" height="24"/>
24-
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
24+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
2525
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
2626
<color key="textColor" red="0.0039215686274509803" green="0.20000000000000001" blue="0.33725490196078434" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
2727
<nil key="highlightedColor"/>
2828
</label>
2929
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="7tN-9S-3pI">
3030
<rect key="frame" x="20" y="49" width="280" height="17"/>
31-
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
31+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
3232
<fontDescription key="fontDescription" type="system" pointSize="14"/>
3333
<color key="textColor" red="0.0039215686274509803" green="0.20000000000000001" blue="0.33725490196078434" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3434
<nil key="highlightedColor"/>
3535
</label>
3636
</subviews>
37-
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
37+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
3838
<constraints>
3939
<constraint firstAttribute="trailing" secondItem="7tN-9S-3pI" secondAttribute="trailing" constant="20" id="6Xi-pn-I9O"/>
4040
<constraint firstItem="7tN-9S-3pI" firstAttribute="leading" secondItem="7TX-v0-D43" secondAttribute="leadingMargin" constant="12" id="Iv8-RH-bpk"/>

Demo/CFAlertViewControllerDemo/View Controller/HomeTableViewController.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414

1515

16+
#define DEFAULT_TITLE_COLOR [UIColor colorWithRed:1.0/255.0 green:51.0/255.0 blue:86.0/255.0 alpha:1.0]
17+
#define DEFAULT_MESSAGE_COLOR [UIColor colorWithRed:1.0/255.0 green:51.0/255.0 blue:86.0/255.0 alpha:1.0]
18+
1619
#define DEFAULT_BTN_TITLE @"DEFAULT"
1720
#define DEFAULT_BTN_COLOR [UIColor colorWithRed:41.0/255.0 green:198.0/255.0 blue:77.0/255.0 alpha:1.0]
1821
#define DEFAULT_BTN_TITLE_COLOR [UIColor whiteColor]
@@ -115,7 +118,9 @@ - (IBAction) showAlertButtonClicked:(id)sender {
115118

116119
// Create Alert
117120
CFAlertViewController *alert = [CFAlertViewController alertControllerWithTitle:titleText
121+
titleColor:DEFAULT_TITLE_COLOR
118122
message:descText
123+
messageColor:DEFAULT_MESSAGE_COLOR
119124
textAlignment:[self getTextAlignment]
120125
preferredStyle:[self getAlertStyle]
121126
headerView:headerView

0 commit comments

Comments
 (0)