Skip to content

Commit

Permalink
Merge pull request #171 from blazierkyle/master
Browse files Browse the repository at this point in the history
Custom Subview
  • Loading branch information
vikmeup committed Apr 29, 2016
2 parents 3b88ff1 + d78b7e2 commit 204f94a
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 15 deletions.
29 changes: 20 additions & 9 deletions Example/SCLAlertViewExample/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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">
<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">
<dependencies>
<deployment defaultVersion="1792" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7026.1"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -18,7 +18,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Uhe-aS-BHL">
<rect key="frame" x="261" y="153" width="77" height="30"/>
<rect key="frame" x="262" y="153" width="76" height="30"/>
<state key="normal" title="Show Error">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
Expand All @@ -27,7 +27,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="dOp-SD-9bQ">
<rect key="frame" x="256" y="203" width="88" height="30"/>
<rect key="frame" x="257" y="203" width="87" height="30"/>
<state key="normal" title="Show Notice">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
Expand All @@ -36,7 +36,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="VWR-vv-K1w">
<rect key="frame" x="250" y="253" width="100" height="30"/>
<rect key="frame" x="251" y="253" width="99" height="30"/>
<state key="normal" title="Show Warning">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
Expand All @@ -45,7 +45,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ism-f4-jWc">
<rect key="frame" x="266" y="303" width="69" height="30"/>
<rect key="frame" x="267" y="303" width="68" height="30"/>
<state key="normal" title="Show Info">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
Expand All @@ -54,16 +54,25 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="arb-3g-Toj">
<rect key="frame" x="249" y="103" width="102" height="30"/>
<rect key="frame" x="250" y="103" width="100" height="30"/>
<state key="normal" title="Show Success">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="showSuccess:" destination="ngj-nl-OHz" eventType="touchUpInside" id="Xuj-bW-sNI"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="9bA-oa-lCU">
<rect key="frame" x="222" y="391" width="157" height="30"/>
<state key="normal" title="Show Custom Subview">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="showCustomSubview:" destination="ngj-nl-OHz" eventType="touchUpInside" id="exk-kY-yZm"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="McK-98-W7e">
<rect key="frame" x="265" y="353" width="70" height="30"/>
<rect key="frame" x="266" y="353" width="69" height="30"/>
<state key="normal" title="Show Edit">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
Expand All @@ -74,6 +83,7 @@
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="9bA-oa-lCU" firstAttribute="centerX" secondItem="7du-np-8iv" secondAttribute="centerX" id="0BR-Pb-lMH"/>
<constraint firstItem="McK-98-W7e" firstAttribute="centerX" secondItem="arb-3g-Toj" secondAttribute="centerX" constant="0.5" id="6if-Ab-Hmf"/>
<constraint firstAttribute="centerX" secondItem="arb-3g-Toj" secondAttribute="centerX" id="6vH-HH-HK4"/>
<constraint firstItem="arb-3g-Toj" firstAttribute="top" secondItem="G4y-kv-ZRs" secondAttribute="bottom" constant="83" id="7Lm-Sx-HKe"/>
Expand All @@ -85,6 +95,7 @@
<constraint firstItem="VWR-vv-K1w" firstAttribute="centerX" secondItem="dOp-SD-9bQ" secondAttribute="centerX" id="R9D-mp-DSX"/>
<constraint firstItem="arb-3g-Toj" firstAttribute="centerX" secondItem="G4y-kv-ZRs" secondAttribute="centerX" id="am6-Lr-rTd"/>
<constraint firstItem="VWR-vv-K1w" firstAttribute="centerX" secondItem="ism-f4-jWc" secondAttribute="centerX" id="fku-3F-Xme"/>
<constraint firstItem="9bA-oa-lCU" firstAttribute="top" secondItem="McK-98-W7e" secondAttribute="bottom" constant="8" id="fph-BA-FU4"/>
<constraint firstItem="dOp-SD-9bQ" firstAttribute="centerX" secondItem="arb-3g-Toj" secondAttribute="centerX" id="gPO-a1-Hre"/>
<constraint firstItem="McK-98-W7e" firstAttribute="leading" secondItem="ism-f4-jWc" secondAttribute="leading" id="kzA-aj-lPP"/>
<constraint firstItem="ism-f4-jWc" firstAttribute="top" secondItem="VWR-vv-K1w" secondAttribute="bottom" constant="20" id="m91-r8-IcB"/>
Expand Down
38 changes: 38 additions & 0 deletions Example/SCLAlertViewExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,44 @@ class ViewController: UIViewController {
}
alert.showEdit(kInfoTitle, subTitle:kSubtitle)
}


@IBAction func showCustomSubview(sender: AnyObject) {
let alert = SCLAlertView()

// Creat the subview
let subview = UIView(frame: CGRectMake(0,0,216,70))
let x = (subview.frame.width - 180) / 2

// Add textfield 1
let textfield1 = UITextField(frame: CGRectMake(x,10,180,25))
textfield1.layer.borderColor = UIColor.greenColor().CGColor
textfield1.layer.borderWidth = 1.5
textfield1.layer.cornerRadius = 5
textfield1.placeholder = "Username"
textfield1.textAlignment = NSTextAlignment.Center
subview.addSubview(textfield1)

// Add textfield 2
let textfield2 = UITextField(frame: CGRectMake(x,textfield1.frame.maxY + 10,180,25))
textfield2.secureTextEntry = true
textfield2.layer.borderColor = UIColor.blueColor().CGColor
textfield2.layer.borderWidth = 1.5
textfield2.layer.cornerRadius = 5
textfield1.layer.borderColor = UIColor.blueColor().CGColor
textfield2.placeholder = "Password"
textfield2.textAlignment = NSTextAlignment.Center
subview.addSubview(textfield2)

// Add the subview to the alert's UI property
alert.customSubview = subview
alert.showCloseButton = false
alert.addButton("Login") {
print("Logged in")
}

alert.showInfo("Login", subTitle: "")
}

func firstButton() {
print("First button tapped")
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,44 @@ alert.addButton("Show Name") {
alert.showEdit("Edit View", subTitle: "This alert view shows a text box")
```

#### Use a custom subview instead of a subtitle
```swift
// Example of using the view to add two text fields to the alert
// Create the subview
let subview = UIView(frame: CGRectMake(0,0,216,70))
let x = (subview.frame.width - 180) / 2

// Add textfield 1
let textfield1 = UITextField(frame: CGRectMake(x,10,180,25))
textfield1.layer.borderColor = UIColor.greenColor().CGColor
textfield1.layer.borderWidth = 1.5
textfield1.layer.cornerRadius = 5
textfield1.placeholder = "Username"
textfield1.textAlignment = NSTextAlignment.Center
subview.addSubview(textfield1)

// Add textfield 2
let textfield2 = UITextField(frame: CGRectMake(x,textfield1.frame.maxY + 10,180,25))
textfield2.secureTextEntry = true
textfield2.layer.borderColor = UIColor.blueColor().CGColor
textfield2.layer.borderWidth = 1.5
textfield2.layer.cornerRadius = 5
textfield1.layer.borderColor = UIColor.blueColor().CGColor
textfield2.placeholder = "Password"
textfield2.textAlignment = NSTextAlignment.Center
subview.addSubview(textfield2)

// Add the subview to the alert's UI property
alert.customSubview = subview
alert.showCloseButton = false
alert.addButton("Login") {
print("Logged in")
}

alert.showInfo("Login", subTitle: "")
```


### Alert View Styles

```swift
Expand Down
23 changes: 17 additions & 6 deletions SCLAlertView/SCLAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public class SCLAlertView: UIViewController {
public var fieldCornerRadius : CGFloat = 3.0
public var buttonCornerRadius : CGFloat = 3.0
public var iconTintColor: UIColor?
public var customSubview : UIView?

// Actions
public var hideWhenBackgroundViewIsTapped = false
Expand Down Expand Up @@ -215,14 +216,24 @@ public class SCLAlertView: UIViewController {
consumedHeight += kTextViewdHeight * CGFloat(input.count)
let maxViewTextHeight = maxHeight - consumedHeight
let viewTextWidth = kWindowWidth - 24
let suggestedViewTextSize = viewText.sizeThatFits(CGSizeMake(viewTextWidth, CGFloat.max))
let viewTextHeight = min(suggestedViewTextSize.height, maxViewTextHeight)
var viewTextHeight = kTextHeight

// scroll management
if (suggestedViewTextSize.height > maxViewTextHeight) {
viewText.scrollEnabled = true
// Check if there is a custom subview and add it over the textview
if let customSubview = customSubview {
viewTextHeight = min(customSubview.frame.height, maxViewTextHeight)
viewText.text = ""
viewText.addSubview(customSubview)
} else {
viewText.scrollEnabled = false
// computing the right size to use for the textView
let suggestedViewTextSize = viewText.sizeThatFits(CGSizeMake(viewTextWidth, CGFloat.max))
viewTextHeight = min(suggestedViewTextSize.height, maxViewTextHeight)

// scroll management
if (suggestedViewTextSize.height > maxViewTextHeight) {
viewText.scrollEnabled = true
} else {
viewText.scrollEnabled = false
}
}

let windowHeight = consumedHeight + viewTextHeight
Expand Down
Binary file added customSubviewScreenshot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 204f94a

Please sign in to comment.