FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Improvements · stringcode86/AlertViewController@4745200 · GitHub

Commit 4745200

Browse files
committed
Improvements
1 parent 849c8e0 commit 4745200

6 files changed

Lines changed: 102 additions & 67 deletions

File tree

‎AlertViewController.xcodeproj/project.pbxproj‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
9F7476F61F288C33000E2B6A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9F7476F41F288C33000E2B6A /* Main.storyboard */; };
1313
9F7476F81F288C33000E2B6A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9F7476F71F288C33000E2B6A /* Assets.xcassets */; };
1414
9F7476FB1F288C33000E2B6A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9F7476F91F288C33000E2B6A /* LaunchScreen.storyboard */; };
15-
9F7477031F288C66000E2B6A /* AlertViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F7477021F288C66000E2B6A /* AlertViewController.swift */; };
16-
9F7477051F28BD76000E2B6A /* UIAlertAction+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F7477041F28BD76000E2B6A /* UIAlertAction+Extensions.swift */; };
15+
9F7477031F288C66000E2B6A /* ImageAlertViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F7477021F288C66000E2B6A /* ImageAlertViewController.swift */; };
1716
/* End PBXBuildFile section */
1817

1918
/* Begin PBXFileReference section */
@@ -25,8 +24,7 @@
2524
9F7476F71F288C33000E2B6A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2625
9F7476FA1F288C33000E2B6A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2726
9F7476FC1F288C33000E2B6A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
28-
9F7477021F288C66000E2B6A /* AlertViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertViewController.swift; sourceTree = "<group>"; };
29-
9F7477041F28BD76000E2B6A /* UIAlertAction+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIAlertAction+Extensions.swift"; sourceTree = "<group>"; };
27+
9F7477021F288C66000E2B6A /* ImageAlertViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageAlertViewController.swift; sourceTree = "<group>"; };
3028
/* End PBXFileReference section */
3129

3230
/* Begin PBXFrameworksBuildPhase section */
@@ -62,8 +60,7 @@
6260
children = (
6361
9F7476F01F288C33000E2B6A /* AppDelegate.swift */,
6462
9F7476F21F288C33000E2B6A /* ViewController.swift */,
65-
9F7477021F288C66000E2B6A /* AlertViewController.swift */,
66-
9F7477041F28BD76000E2B6A /* UIAlertAction+Extensions.swift */,
63+
9F7477021F288C66000E2B6A /* ImageAlertViewController.swift */,
6764
9F7476F41F288C33000E2B6A /* Main.storyboard */,
6865
9F7476F71F288C33000E2B6A /* Assets.xcassets */,
6966
9F7476F91F288C33000E2B6A /* LaunchScreen.storyboard */,
@@ -104,7 +101,7 @@
104101
TargetAttributes = {
105102
9F7476EC1F288C33000E2B6A = {
106103
CreatedOnToolsVersion = 8.3.3;
107-
DevelopmentTeam = T8TH53QYXW;
104+
DevelopmentTeam = J4Z2NCTR3S;
108105
ProvisioningStyle = Automatic;
109106
};
110107
};
@@ -145,9 +142,8 @@
145142
isa = PBXSourcesBuildPhase;
146143
buildActionMask = 2147483647;
147144
files = (
148-
9F7477051F28BD76000E2B6A /* UIAlertAction+Extensions.swift in Sources */,
149145
9F7476F31F288C33000E2B6A /* ViewController.swift in Sources */,
150-
9F7477031F288C66000E2B6A /* AlertViewController.swift in Sources */,
146+
9F7477031F288C66000E2B6A /* ImageAlertViewController.swift in Sources */,
151147
9F7476F11F288C33000E2B6A /* AppDelegate.swift in Sources */,
152148
);
153149
runOnlyForDeploymentPostprocessing = 0;
@@ -273,7 +269,7 @@
273269
isa = XCBuildConfiguration;
274270
buildSettings = {
275271
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
276-
DEVELOPMENT_TEAM = T8TH53QYXW;
272+
DEVELOPMENT_TEAM = J4Z2NCTR3S;
277273
INFOPLIST_FILE = AlertViewController/Info.plist;
278274
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
279275
PRODUCT_BUNDLE_IDENTIFIER = uk.co.stringcode.AlertViewController;
@@ -286,7 +282,7 @@
286282
isa = XCBuildConfiguration;
287283
buildSettings = {
288284
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
289-
DEVELOPMENT_TEAM = T8TH53QYXW;
285+
DEVELOPMENT_TEAM = J4Z2NCTR3S;
290286
INFOPLIST_FILE = AlertViewController/Info.plist;
291287
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
292288
PRODUCT_BUNDLE_IDENTIFIER = uk.co.stringcode.AlertViewController;

‎AlertViewController/Assets.xcassets/AppIcon.appiconset/Contents.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
"idiom" : "ipad",
8585
"size" : "83.5x83.5",
8686
"scale" : "2x"
87+
},
88+
{
89+
"idiom" : "ios-marketing",
90+
"size" : "1024x1024",
91+
"scale" : "1x"
8792
}
8893
],
8994
"info" : {

‎AlertViewController/Base.lproj/Main.storyboard‎

Lines changed: 11 additions & 2 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.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16G29" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
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="12089"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1010
</dependencies>
1111
<scenes>
@@ -28,11 +28,20 @@
2828
<action selector="doStuffAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="yGK-g7-5Nk"/>
2929
</connections>
3030
</button>
31+
<button opaque="NO" tag="1" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="gjT-Pu-Ljd">
32+
<rect key="frame" x="124" y="386" width="126" height="30"/>
33+
<state key="normal" title="Show ActionSheet"/>
34+
<connections>
35+
<action selector="doStuffAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="ZMW-bj-JQb"/>
36+
</connections>
37+
</button>
3138
</subviews>
3239
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3340
<constraints>
41+
<constraint firstItem="gjT-Pu-Ljd" firstAttribute="top" secondItem="hew-63-qSi" secondAttribute="bottom" constant="37.5" id="pFO-l4-GNX"/>
3442
<constraint firstItem="hew-63-qSi" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="pgn-xm-bYo"/>
3543
<constraint firstItem="hew-63-qSi" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="rbr-61-WNu"/>
44+
<constraint firstItem="gjT-Pu-Ljd" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="wXG-R7-bAW"/>
3645
</constraints>
3746
</view>
3847
</viewController>

AlertViewController/AlertViewController.swift renamed to AlertViewController/ImageAlertViewController.swift

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,48 @@
88

99
import UIKit
1010

11+
12+
// MARK: - UIAlertController
13+
1114
/// Adds ability to display `UIImage` above the title label of `UIAlertController`.
1215
/// Functionality is achieved by adding “\n” characters to `title`, to make space
1316
/// for `UIImageView` to be added to `UIAlertController.view`. Set `title` as
1417
/// normal but when retrieving value use `originalTitle` property.
15-
class AlertController: UIAlertController {
18+
class ImageAlertController: UIAlertController {
1619
/// - Return: value that was set on `title`
1720
private(set) var originalTitle: String?
1821
private var spaceAdjustedTitle: String = ""
1922
private weak var imageView: UIImageView? = nil
2023
private var previousImgViewSize: CGSize = .zero
21-
24+
private var imageViewSize: CGSize?
25+
26+
/// - parameter image: `UIImage` to be displayed about title label
27+
var titleImage: UIImage? {
28+
set {
29+
guard let imageView = self.imageView else {
30+
let imageView = UIImageView(image: newValue)
31+
if let size = imageViewSize {
32+
imageView.frame.size = size
33+
}
34+
self.view.addSubview(imageView)
35+
self.imageView = imageView
36+
return
37+
}
38+
imageView.image = newValue
39+
}
40+
get {
41+
return imageView?.image
42+
}
43+
}
44+
45+
convenience init(image: UIImage?, size: CGSize? = nil, title: String?, message: String?, preferredStyle: UIAlertControllerStyle) {
46+
self.init(title: title, message: message, preferredStyle: preferredStyle)
47+
48+
guard let image = image else { return }
49+
imageViewSize = size
50+
titleImage = image
51+
}
52+
2253
override var title: String? {
2354
didSet {
2455
// Keep track of original title
@@ -28,17 +59,6 @@ class AlertController: UIAlertController {
2859
}
2960
}
3061

31-
/// - parameter image: `UIImage` to be displayed about title label
32-
func setTitleImage(_ image: UIImage?) {
33-
guard let imageView = self.imageView else {
34-
let imageView = UIImageView(image: image)
35-
self.view.addSubview(imageView)
36-
self.imageView = imageView
37-
return
38-
}
39-
imageView.image = image
40-
}
41-
4262
// MARK: - Layout code
4363

4464
override func viewDidLayoutSubviews() {
@@ -80,9 +100,43 @@ class AlertController: UIAlertController {
80100

81101
struct Constants {
82102
static var paddingAlert: CGFloat = 22
83-
static var paddingSheet: CGFloat = 11
103+
static var paddingSheet: CGFloat = 16
84104
static func padding(for style: UIAlertControllerStyle) -> CGFloat {
85105
return style == .alert ? Constants.paddingAlert : Constants.paddingSheet
86106
}
87107
}
88108
}
109+
110+
111+
// MARK: - UIAlertAction
112+
113+
/// Adds ability to display image left of the action title, by leveraging KVC.
114+
/// Also checks whether `UIAlertAction` responds to appropriate selector to
115+
/// avoid crashes if property is not available in the future.
116+
extension UIAlertAction {
117+
118+
convenience init(title: String?, image: UIImage?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Swift.Void)? = nil) {
119+
self.init(title: title, style: style, handler: handler)
120+
121+
actionImage = image
122+
}
123+
124+
/// Image to display left of the action title
125+
var actionImage: UIImage? {
126+
get {
127+
if self.responds(to: Selector(Constants.imageKey)) {
128+
return self.value(forKey: Constants.imageKey) as? UIImage
129+
}
130+
return nil
131+
}
132+
set {
133+
if self.responds(to: Selector(Constants.imageKey)) {
134+
self.setValue(newValue, forKey: Constants.imageKey)
135+
}
136+
}
137+
}
138+
139+
private struct Constants {
140+
static var imageKey = "image"
141+
}
142+
}

‎AlertViewController/UIAlertAction+Extensions.swift‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎AlertViewController/ViewController.swift‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ import UIKit
1010

1111
class ViewController: UIViewController {
1212

13-
@IBAction func doStuffAction(_ sender: Any) {
14-
// Create AlertController
15-
let alert = AlertController(title: "Tesing", message: "Wubba lubba dub dub", preferredStyle: .alert)
16-
alert.setTitleImage(UIImage(named: "alert"))
13+
@IBAction func doStuffAction(_ sender: UIButton) {
14+
let style: UIAlertControllerStyle = sender.tag == 0 ? .alert : .actionSheet
15+
let alert = ImageAlertController(image: UIImage(named: "alert"), size: CGSize(width: 44, height: 44), title: "Tesing", message: "Wubba lubba dub dub", preferredStyle: style)
16+
17+
if sender.tag > 0 {
18+
alert.popoverPresentationController?.sourceView = sender
19+
alert.popoverPresentationController?.sourceRect = sender.bounds
20+
alert.modalPresentationStyle = .popover
21+
}
22+
1723
// Add actions
18-
let action = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
19-
action.actionImage = UIImage(named: "close")
24+
let action = UIAlertAction(title: "Cancel", image: UIImage(named: "close"), style: .cancel, handler: nil)
2025
alert.addAction(UIAlertAction(title: "Default", style: .default, handler: nil))
2126
alert.addAction(UIAlertAction(title: "Destroy", style: .destructive, handler: nil))
2227
alert.addAction(action)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL