Skip to content

Commit

Permalink
Added better iPad/iPhone support + removed constants
Browse files Browse the repository at this point in the history
- Grouped all constants into a configurable struct.
- Added better support iPhones and iPads.
- Fixed bug where if dialog is presented in a navigation controller the fonts appear small.
  • Loading branch information
Minitour committed Mar 13, 2019
1 parent 894b93d commit ae735fd
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 34 deletions.
2 changes: 1 addition & 1 deletion AZDialogView.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "AZDialogView"
s.version = "1.3.0"
s.version = "1.3.1"
s.summary = "A highly customizable alert dialog controller that mimics Snapchat's alert dialog."
s.homepage = "https://github.com/Minitour/AZDialogViewController"
s.license = "MIT"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="XZv-hM-Klq">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
<capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -55,7 +52,7 @@
</connections>
</button>
<button opaque="NO" tag="3" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="3YO-bR-uly">
<rect key="frame" x="97" y="362" width="180" height="30"/>
<rect key="frame" x="97.5" y="362.5" width="180" height="30"/>
<constraints>
<constraint firstAttribute="width" constant="180" id="PXu-vC-gSv"/>
</constraints>
Expand All @@ -77,10 +74,11 @@
<constraint firstItem="46Q-bz-5vc" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="r3s-9s-v1m"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="wV2-iZ-0sl"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="136.80000000000001" y="138.98050974512745"/>
<point key="canvasLocation" x="1076" y="138.98050974512745"/>
</scene>
<!--View Controller-->
<scene sceneID="wVI-Lz-wSZ">
Expand All @@ -95,27 +93,27 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="hLx-cl-ARl">
<rect key="frame" x="32" y="237" width="311" height="192"/>
<rect key="frame" x="32" y="237.5" width="311" height="192"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ort-1Y-R1l">
<rect key="frame" x="109" y="-50" width="93.5" height="93"/>
<rect key="frame" x="109" y="-50" width="93" height="93.5"/>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="width" secondItem="ort-1Y-R1l" secondAttribute="height" multiplier="1:1" id="NgU-c3-j7M"/>
</constraints>
</view>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="Uun-It-7X9">
<rect key="frame" x="8" y="51" width="295" height="133"/>
<rect key="frame" x="8" y="51.5" width="295" height="132.5"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Lbn-uy-y5e">
<rect key="frame" x="27.5" y="0.0" width="240" height="132"/>
<rect key="frame" x="27.5" y="0.0" width="240" height="131.5"/>
<color key="backgroundColor" red="0.0" green="0.69491332769393921" blue="0.40852432227078822" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="s1V-GE-Q43">
<rect key="frame" x="27.5" y="132" width="240" height="1"/>
<rect key="frame" x="27.5" y="131.5" width="240" height="1"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="height" constant="1" id="xcg-ba-Gyl"/>
Expand Down Expand Up @@ -150,7 +148,25 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="siW-fB-THV" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="981.60000000000002" y="138.98050974512745"/>
<point key="canvasLocation" x="1920.8" y="138.98050974512745"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="mPh-FF-rPK">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="XZv-hM-Klq" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="upM-gr-ex3">
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="hDw-TS-Vgo"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="X5t-G0-RQA" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="136.80000000000001" y="138.98050974512745"/>
</scene>
</scenes>
</document>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
DialogDefaults.widthRatio = 1
// Do any additional setup after loading the view, typically from a nib.
}

Expand Down
76 changes: 56 additions & 20 deletions Sources/AZDialogViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ import UIKit

public typealias ActionHandler = ((AZDialogViewController)->(Void))

public struct DialogDefaults {

/// The spaving ratio between the items in the stack (vertically) in respect to the height of the device.
public static var spacingRatio: CGFloat = 0.012

/// The font size of the title in respect to the device height.
public static var titleFontSizeRatio: CGFloat = 0.0275

/// The font size of the message in respect to the device height.
public static var messageFontSizeRatio: CGFloat = 0.0225

/// The height of the seperator.
public static var seperatorHeight: CGFloat = 0.7

/// The width of the buttons in respect to the device width.
public static var buttonWidthRatio: CGFloat = 0.65

/// The height of the button in respect to the height of the device.
public static var buttonHeightRatio: CGFloat = 0.07

/// The height of the cancel button in respect to the height of the device.
public static var cancelButtonHeightRatio: CGFloat = 0.0449

/// The width ratio of the dialog view in respect to the width of the device.
public static var widthRatio: CGFloat = 0.75
}

@objc
open class AZDialogViewController: UIViewController{

Expand Down Expand Up @@ -124,16 +151,21 @@ open class AZDialogViewController: UIViewController{

// Helper to get the real device width
fileprivate var deviceWidth: CGFloat {
let view = UIScreen.main
let realValue = (view.bounds.width < view.bounds.height ? view.bounds.width : view.bounds.height)
let value = (realValue > 414 ? realValue / 2 : realValue)
//let value = (realValue > 414 ? realValue / 2 : realValue)
let value = UIDevice.current.userInterfaceIdiom == .pad ? realValue / 2 : realValue
return value
}

// Helper to get the real device height
fileprivate var deviceHeight: CGFloat {
let view = UIScreen.main
let safeAreaRemoval = parentSafeArea.sum
let realValue = (view.bounds.width < view.bounds.height ? view.bounds.height : view.bounds.width) - safeAreaRemoval
let value = ((realValue > 736 && realValue < 818) ? realValue / 2 : realValue)

//let value = ((realValue > 736 && realValue < 818) ? realValue / 2 : realValue)
let value = UIDevice.current.userInterfaceIdiom == .pad ? realValue / 2 : realValue
return value
}

Expand Down Expand Up @@ -423,7 +455,7 @@ open class AZDialogViewController: UIViewController{
open var estimatedHeight: CGFloat {

if isViewLoaded{
return baseView.frame.height
return baseView.bounds.height
}

func heightForView(_ text:String, font:UIFont, width:CGFloat) -> CGFloat{
Expand All @@ -450,31 +482,31 @@ open class AZDialogViewController: UIViewController{
let showSeparator = self.showSeparator
let mTitle = self.mTitle
let mMessage = self.mMessage
let spacing = height * 0.012
let spacing = height * DialogDefaults.spacingRatio
let sideSpacing: CGFloat = 20.0
let showImage = image != nil
let side: CGFloat = width / 8
let labelWidth: CGFloat = width - side * 2 - sideSpacing
let imageHolderSize: CGFloat = showImage ? CGFloat(Int((width - 2 * side) / 3)) : 0
let imageMultiplier:CGFloat = showImage ? 0.0 : 1.0

titleFontSize = height * 0.0269
titleFontSize = height * DialogDefaults.titleFontSizeRatio
let titleFont = UIFont(name: fontNameBold, size: titleFontSize)
let titleHeight:CGFloat = mTitle == nil ? 0.0 : heightForView(mTitle!, font: titleFont!, width: labelWidth)

let seperatorHeight: CGFloat = showSeparator ? 0.7 : 0.0
let seperatorHeight: CGFloat = showSeparator ? DialogDefaults.seperatorHeight : 0.0
let seperatorMultiplier: CGFloat = seperatorHeight > 0.0 ? 1.0 : 0.0

messageFontSize = height * 0.0239
messageFontSize = height * DialogDefaults.messageFontSizeRatio
let labelFont = UIFont(name: fontName, size: messageFontSize)!
let messageLableHeight:CGFloat = mMessage == nil ? 0 : heightForView(mMessage!, font: labelFont, width: labelWidth)
let messageLabelMultiplier: CGFloat = messageLableHeight > 0 ? 1.0 : 0.0

buttonHeight = CGFloat(Int(height * 0.07))
buttonHeight = CGFloat(Int(height * DialogDefaults.buttonHeightRatio))
let stackViewSize: CGFloat = CGFloat(self.actions.count) * buttonHeight + CGFloat(self.actions.count-1) * (stackSpacing)
let stackMultiplier:CGFloat = stackViewSize > 0 ? 1.0 : 0.0

cancelButtonHeight = height * 0.0449
cancelButtonHeight = height * DialogDefaults.cancelButtonHeightRatio
let cancelMultiplier: CGFloat = cancelEnabled ? 1.0 : 0.0

// Elaboration on spacingCalc:
Expand All @@ -498,7 +530,7 @@ open class AZDialogViewController: UIViewController{
var baseViewHeight:CGFloat = 0.0

baseViewHeight += spacingCalc
baseViewHeight += (2 * imageHolderSize/3)
baseViewHeight += (2 * imageHolderSize / 3)
baseViewHeight += titleHeight
baseViewHeight += seperatorHeight
baseViewHeight += messageLableHeight
Expand Down Expand Up @@ -624,7 +656,7 @@ open class AZDialogViewController: UIViewController{
leftToolItem = UIButton(type: .system)
rightToolItem = UIButton(type: .system)

if spacing == -1 {spacing = deviceHeight * 0.012}
if spacing == -1 {spacing = deviceHeight * DialogDefaults.spacingRatio}
let showImage = imageHandler?(imageView) ?? false

// Disable translate auto resizing mask into constraints
Expand Down Expand Up @@ -714,7 +746,12 @@ open class AZDialogViewController: UIViewController{
self.view.insertSubview(blurView, belowSubview: baseView)
}

UIView.animate(withDuration: animationDuration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 6.0, options: [], animations: { [weak self]() -> Void in
UIView.animate(withDuration: animationDuration,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 6.0,
options: [],
animations: { [weak self]() -> Void in
if let `self` = self {
self.baseView.center = self.view.center
self.baseView.center.y = self.baseView.center.y + self.contentOffset
Expand Down Expand Up @@ -998,7 +1035,7 @@ open class AZDialogViewController: UIViewController{
/// Setup Title Label
fileprivate func setupTitleLabel(){

if titleFontSize == 0 {titleFontSize = deviceHeight * 0.0275}
if titleFontSize == 0 { titleFontSize = deviceHeight * DialogDefaults.titleFontSizeRatio }
let titleFont = UIFont(name: fontNameBold, size: titleFontSize)
//let titleHeight:CGFloat = mTitle == nil ? 0 : heightForView(mTitle!, font: titleFont!, width: deviceWidth * 0.6)
titleLabel.numberOfLines = 0
Expand All @@ -1014,15 +1051,15 @@ open class AZDialogViewController: UIViewController{

/// Setup Seperator Line
fileprivate func setupSeparator(){
let seperatorHeight: CGFloat = self.showSeparator ? 0.7 : 0.0
let seperatorHeight: CGFloat = self.showSeparator ? DialogDefaults.seperatorHeight : 0.0
separatorView.backgroundColor = separatorColor
separatorView.widthAnchor.constraint(equalTo: titleLabel.widthAnchor, multiplier: 1.0).isActive = true
separatorView.heightAnchor.constraint(equalToConstant: seperatorHeight).isActive = true
}

/// Setup Message Label
fileprivate func setupMessageLabel(){
if messageFontSize == 0 {messageFontSize = deviceHeight * 0.0225}
if messageFontSize == 0 {messageFontSize = deviceHeight * DialogDefaults.messageFontSizeRatio}
let labelFont = UIFont(name: fontName, size: messageFontSize)!
messageLabel.numberOfLines = 0
messageLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
Expand All @@ -1047,7 +1084,7 @@ open class AZDialogViewController: UIViewController{
buttonsStackView.alignment = .fill
buttonsStackView.axis = .vertical
buttonsStackView.spacing = stackSpacing
buttonsStackView.widthAnchor.constraint(equalTo: baseView.widthAnchor, multiplier: 0.65).isActive = true
buttonsStackView.widthAnchor.constraint(equalTo: baseView.widthAnchor, multiplier: DialogDefaults.buttonWidthRatio).isActive = true

for i in 0 ..< actions.count{
let button = setupButton(index: i)
Expand All @@ -1061,7 +1098,7 @@ open class AZDialogViewController: UIViewController{
/// - index: The index for the current button.
///
fileprivate func setupButton(index i:Int) -> UIButton{
if buttonHeight == 0 {buttonHeight = CGFloat(Int(deviceHeight * 0.07))}
if buttonHeight == 0 {buttonHeight = CGFloat(Int(deviceHeight * DialogDefaults.buttonHeightRatio))}

let finButton: UIButton

Expand Down Expand Up @@ -1097,7 +1134,7 @@ open class AZDialogViewController: UIViewController{

/// Setup Cancel Button
fileprivate func setupCancelButton(){
if cancelButtonHeight == 0 {cancelButtonHeight = deviceHeight * 0.0449}
if cancelButtonHeight == 0 {cancelButtonHeight = deviceHeight * DialogDefaults.cancelButtonHeightRatio}
cancelButton.setTitle(cancelTitle, for: [])
cancelButton.titleLabel?.font = UIFont(name: fontName, size: cancelButtonHeight * 0.433)
let showCancelButton = (cancelButtonStyle?(cancelButton,cancelButtonHeight) ?? false) && cancelEnabled
Expand All @@ -1114,7 +1151,7 @@ open class AZDialogViewController: UIViewController{
/// Setup BaseView
fileprivate func setupBaseView(){
self.baseView.isExclusiveTouch = true
baseView.widthAnchor.constraint(equalToConstant: deviceWidth * 0.75).isActive = true
baseView.widthAnchor.constraint(equalToConstant: deviceWidth * DialogDefaults.widthRatio).isActive = true
baseView.centerXAnchor.constraint(equalTo: view.centerXAnchor,constant: 0).isActive = true
baseViewCenterYConstraint =
baseView.centerYAnchor.constraint(equalTo: view.centerYAnchor,constant: contentOffset)
Expand Down Expand Up @@ -1312,4 +1349,3 @@ fileprivate extension UIVisualEffectView {
}

}

0 comments on commit ae735fd

Please sign in to comment.