Skip to content

Commit 204f94a

Browse files
committed
Merge pull request #171 from blazierkyle/master
Custom Subview
2 parents 3b88ff1 + d78b7e2 commit 204f94a

File tree

5 files changed

+113
-15
lines changed

5 files changed

+113
-15
lines changed

Example/SCLAlertViewExample/Main.storyboard

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6206.9" systemVersion="14A329r" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="ngj-nl-OHz">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="ngj-nl-OHz">
33
<dependencies>
4-
<deployment defaultVersion="1792" identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7026.1"/>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
66
</dependencies>
77
<scenes>
88
<!--View Controller-->
@@ -18,7 +18,7 @@
1818
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
1919
<subviews>
2020
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Uhe-aS-BHL">
21-
<rect key="frame" x="261" y="153" width="77" height="30"/>
21+
<rect key="frame" x="262" y="153" width="76" height="30"/>
2222
<state key="normal" title="Show Error">
2323
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
2424
</state>
@@ -27,7 +27,7 @@
2727
</connections>
2828
</button>
2929
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="dOp-SD-9bQ">
30-
<rect key="frame" x="256" y="203" width="88" height="30"/>
30+
<rect key="frame" x="257" y="203" width="87" height="30"/>
3131
<state key="normal" title="Show Notice">
3232
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
3333
</state>
@@ -36,7 +36,7 @@
3636
</connections>
3737
</button>
3838
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="VWR-vv-K1w">
39-
<rect key="frame" x="250" y="253" width="100" height="30"/>
39+
<rect key="frame" x="251" y="253" width="99" height="30"/>
4040
<state key="normal" title="Show Warning">
4141
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
4242
</state>
@@ -45,7 +45,7 @@
4545
</connections>
4646
</button>
4747
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ism-f4-jWc">
48-
<rect key="frame" x="266" y="303" width="69" height="30"/>
48+
<rect key="frame" x="267" y="303" width="68" height="30"/>
4949
<state key="normal" title="Show Info">
5050
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
5151
</state>
@@ -54,16 +54,25 @@
5454
</connections>
5555
</button>
5656
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="arb-3g-Toj">
57-
<rect key="frame" x="249" y="103" width="102" height="30"/>
57+
<rect key="frame" x="250" y="103" width="100" height="30"/>
5858
<state key="normal" title="Show Success">
5959
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
6060
</state>
6161
<connections>
6262
<action selector="showSuccess:" destination="ngj-nl-OHz" eventType="touchUpInside" id="Xuj-bW-sNI"/>
6363
</connections>
6464
</button>
65+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="9bA-oa-lCU">
66+
<rect key="frame" x="222" y="391" width="157" height="30"/>
67+
<state key="normal" title="Show Custom Subview">
68+
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
69+
</state>
70+
<connections>
71+
<action selector="showCustomSubview:" destination="ngj-nl-OHz" eventType="touchUpInside" id="exk-kY-yZm"/>
72+
</connections>
73+
</button>
6574
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="McK-98-W7e">
66-
<rect key="frame" x="265" y="353" width="70" height="30"/>
75+
<rect key="frame" x="266" y="353" width="69" height="30"/>
6776
<state key="normal" title="Show Edit">
6877
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
6978
</state>
@@ -74,6 +83,7 @@
7483
</subviews>
7584
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
7685
<constraints>
86+
<constraint firstItem="9bA-oa-lCU" firstAttribute="centerX" secondItem="7du-np-8iv" secondAttribute="centerX" id="0BR-Pb-lMH"/>
7787
<constraint firstItem="McK-98-W7e" firstAttribute="centerX" secondItem="arb-3g-Toj" secondAttribute="centerX" constant="0.5" id="6if-Ab-Hmf"/>
7888
<constraint firstAttribute="centerX" secondItem="arb-3g-Toj" secondAttribute="centerX" id="6vH-HH-HK4"/>
7989
<constraint firstItem="arb-3g-Toj" firstAttribute="top" secondItem="G4y-kv-ZRs" secondAttribute="bottom" constant="83" id="7Lm-Sx-HKe"/>
@@ -85,6 +95,7 @@
8595
<constraint firstItem="VWR-vv-K1w" firstAttribute="centerX" secondItem="dOp-SD-9bQ" secondAttribute="centerX" id="R9D-mp-DSX"/>
8696
<constraint firstItem="arb-3g-Toj" firstAttribute="centerX" secondItem="G4y-kv-ZRs" secondAttribute="centerX" id="am6-Lr-rTd"/>
8797
<constraint firstItem="VWR-vv-K1w" firstAttribute="centerX" secondItem="ism-f4-jWc" secondAttribute="centerX" id="fku-3F-Xme"/>
98+
<constraint firstItem="9bA-oa-lCU" firstAttribute="top" secondItem="McK-98-W7e" secondAttribute="bottom" constant="8" id="fph-BA-FU4"/>
8899
<constraint firstItem="dOp-SD-9bQ" firstAttribute="centerX" secondItem="arb-3g-Toj" secondAttribute="centerX" id="gPO-a1-Hre"/>
89100
<constraint firstItem="McK-98-W7e" firstAttribute="leading" secondItem="ism-f4-jWc" secondAttribute="leading" id="kzA-aj-lPP"/>
90101
<constraint firstItem="ism-f4-jWc" firstAttribute="top" secondItem="VWR-vv-K1w" secondAttribute="bottom" constant="20" id="m91-r8-IcB"/>

Example/SCLAlertViewExample/ViewController.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,44 @@ class ViewController: UIViewController {
6767
}
6868
alert.showEdit(kInfoTitle, subTitle:kSubtitle)
6969
}
70+
71+
72+
@IBAction func showCustomSubview(sender: AnyObject) {
73+
let alert = SCLAlertView()
74+
75+
// Creat the subview
76+
let subview = UIView(frame: CGRectMake(0,0,216,70))
77+
let x = (subview.frame.width - 180) / 2
78+
79+
// Add textfield 1
80+
let textfield1 = UITextField(frame: CGRectMake(x,10,180,25))
81+
textfield1.layer.borderColor = UIColor.greenColor().CGColor
82+
textfield1.layer.borderWidth = 1.5
83+
textfield1.layer.cornerRadius = 5
84+
textfield1.placeholder = "Username"
85+
textfield1.textAlignment = NSTextAlignment.Center
86+
subview.addSubview(textfield1)
87+
88+
// Add textfield 2
89+
let textfield2 = UITextField(frame: CGRectMake(x,textfield1.frame.maxY + 10,180,25))
90+
textfield2.secureTextEntry = true
91+
textfield2.layer.borderColor = UIColor.blueColor().CGColor
92+
textfield2.layer.borderWidth = 1.5
93+
textfield2.layer.cornerRadius = 5
94+
textfield1.layer.borderColor = UIColor.blueColor().CGColor
95+
textfield2.placeholder = "Password"
96+
textfield2.textAlignment = NSTextAlignment.Center
97+
subview.addSubview(textfield2)
98+
99+
// Add the subview to the alert's UI property
100+
alert.customSubview = subview
101+
alert.showCloseButton = false
102+
alert.addButton("Login") {
103+
print("Logged in")
104+
}
105+
106+
alert.showInfo("Login", subTitle: "")
107+
}
70108

71109
func firstButton() {
72110
print("First button tapped")

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,44 @@ alert.addButton("Show Name") {
112112
alert.showEdit("Edit View", subTitle: "This alert view shows a text box")
113113
```
114114

115+
#### Use a custom subview instead of a subtitle
116+
```swift
117+
// Example of using the view to add two text fields to the alert
118+
// Create the subview
119+
let subview = UIView(frame: CGRectMake(0,0,216,70))
120+
let x = (subview.frame.width - 180) / 2
121+
122+
// Add textfield 1
123+
let textfield1 = UITextField(frame: CGRectMake(x,10,180,25))
124+
textfield1.layer.borderColor = UIColor.greenColor().CGColor
125+
textfield1.layer.borderWidth = 1.5
126+
textfield1.layer.cornerRadius = 5
127+
textfield1.placeholder = "Username"
128+
textfield1.textAlignment = NSTextAlignment.Center
129+
subview.addSubview(textfield1)
130+
131+
// Add textfield 2
132+
let textfield2 = UITextField(frame: CGRectMake(x,textfield1.frame.maxY + 10,180,25))
133+
textfield2.secureTextEntry = true
134+
textfield2.layer.borderColor = UIColor.blueColor().CGColor
135+
textfield2.layer.borderWidth = 1.5
136+
textfield2.layer.cornerRadius = 5
137+
textfield1.layer.borderColor = UIColor.blueColor().CGColor
138+
textfield2.placeholder = "Password"
139+
textfield2.textAlignment = NSTextAlignment.Center
140+
subview.addSubview(textfield2)
141+
142+
// Add the subview to the alert's UI property
143+
alert.customSubview = subview
144+
alert.showCloseButton = false
145+
alert.addButton("Login") {
146+
print("Logged in")
147+
}
148+
149+
alert.showInfo("Login", subTitle: "")
150+
```
151+
152+
115153
### Alert View Styles
116154

117155
```swift

SCLAlertView/SCLAlertView.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public class SCLAlertView: UIViewController {
123123
public var fieldCornerRadius : CGFloat = 3.0
124124
public var buttonCornerRadius : CGFloat = 3.0
125125
public var iconTintColor: UIColor?
126+
public var customSubview : UIView?
126127

127128
// Actions
128129
public var hideWhenBackgroundViewIsTapped = false
@@ -215,14 +216,24 @@ public class SCLAlertView: UIViewController {
215216
consumedHeight += kTextViewdHeight * CGFloat(input.count)
216217
let maxViewTextHeight = maxHeight - consumedHeight
217218
let viewTextWidth = kWindowWidth - 24
218-
let suggestedViewTextSize = viewText.sizeThatFits(CGSizeMake(viewTextWidth, CGFloat.max))
219-
let viewTextHeight = min(suggestedViewTextSize.height, maxViewTextHeight)
219+
var viewTextHeight = kTextHeight
220220

221-
// scroll management
222-
if (suggestedViewTextSize.height > maxViewTextHeight) {
223-
viewText.scrollEnabled = true
221+
// Check if there is a custom subview and add it over the textview
222+
if let customSubview = customSubview {
223+
viewTextHeight = min(customSubview.frame.height, maxViewTextHeight)
224+
viewText.text = ""
225+
viewText.addSubview(customSubview)
224226
} else {
225-
viewText.scrollEnabled = false
227+
// computing the right size to use for the textView
228+
let suggestedViewTextSize = viewText.sizeThatFits(CGSizeMake(viewTextWidth, CGFloat.max))
229+
viewTextHeight = min(suggestedViewTextSize.height, maxViewTextHeight)
230+
231+
// scroll management
232+
if (suggestedViewTextSize.height > maxViewTextHeight) {
233+
viewText.scrollEnabled = true
234+
} else {
235+
viewText.scrollEnabled = false
236+
}
226237
}
227238

228239
let windowHeight = consumedHeight + viewTextHeight

customSubviewScreenshot1.png

21.3 KB
Loading

0 commit comments

Comments
 (0)