Latest 1.4.0
Homepage https://github.com/ark-develop/iOS-Utils
License MIT
Platforms ios 11.0

Version
Swift 5
Platforms
Carthage compatible
License

The purpose of iOS-Utils is to bring together a bunch of useful utility classes, extensions, and protocols to make iOS development a little easier.

Requirements

  • iOS 11.0+
  • Xcode 10.2+
  • Swift 5.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects.

To integrate iOS-Utils into your Xcode project using CocoaPods, add the following to your Podfile:

pod 'iOS-Utils', '~> 1.4'

Because integrating the entire pod may be overkill, you can alternatively specify which sub specs you want to include.

Sub Spec Utilities Included
Designable DesignableButton, DesignableView
AlertController UIAlertController+Convenience, UIAlertController+UIWindow
Keyboard KeyboardNotification, KeyboardObservable, KeyboardRespondable

To include one or more specific sub modules in you code, specify the dependencies in the Podfile as follows for each sub spec required:

pod 'iOS-Utils/{Sub Spec}', '~> 1.4'

For example, to include the Designable classes in your project, add the following to your Podfile:

pod 'iOS-Utils/Designable', '~> 1.4'

Carthage

Carthage is a dependency manager for Cocoa applications that provides binary frameworks while allowing full control over project structure and setup.

To integrate iOS-Utils into your Xcode project using Carthage, create a Cartfile and add the following:

github "ark-develop/iOS-Utils" ~> 1.4

Utilities

DesignableView & DesignableButton

DesignableView (UIControl) and DesignableButton (UIButton) are IBDesignable views that allow manipulation of properties such as corner radius or drop shadow in Interface Builder, and displays those changes as they are made.

IBInspectable properties:

  • cornerRadius
  • roundTopLeft
  • roundTopRight
  • roundBottomLeft
  • roundBottomRight
  • borderColor
  • borderWidth
  • shadowColor
  • shadowOffset
  • shadowOpacity
  • shadowRadius
  • gradientStartColor
  • gradientStartPoint
  • gradientEndColor
  • gradientEndPoint

ClassName

ClassName is a simple extension on NSObject that adds a className property to any NSObject as a static variable and instance variable.

let className = YourClass.className // "YourClass"
let classInstance = YourClass()
let className = classInstance.className // "YourClass"

UIAlertController+Convenience

UIAlertController+Convenience is an extension for UIAlertController that allows for easier creation of UIAlertControllers while also making it a bit easier to read.

Standard UIAlertController creation:

let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)

let okAction = UIAlertAction(title: "Okay", style: .default, handler: { _ in
    // Okay pressed
})

let cancelAction = UIAlertController(title: "Cancel", style: .cancel, handler: { _ in
    // Cancel pressed
})

alert.addAction(okAction)
alert.addAction(cancelAction)

Convenience UIAlertController creation:

let alert = UIAlertController(title: "title",
                              message: "message",
                              preferredStyle: .alert,
                              alertActions: [
                                  .okay({ _ in
                                      // Okay Pressed
                                  }),
                                  .cancel({ _ in
                                      // Cancel Pressed
                                  }),
                              ])

or

let okAction: AlertAction = .okay({ _ in
    // Okay pressed
})

let cancelAction: AlertAction = .cancel({ _ in
    // Cancel pressed
})

let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert, alertActions: [okAction, cancelAction])

UIAlertController+UIWindow

UIAlertController+UIWindow is an extension for UIAlertController that allows for the presentation of UIAlertControllers similar to how UIAlertView used to work. With this implementation, it is possible to present an alert from anywhere, as a UIViewController (or subclass of) is not required. A separate UIWindow is created on invocation, and the alert is presented in that window. When the alert is dismissed, the application’s window is set back to the key window.

UIAlertController+UIWindow works with UIAlertController’s with a preferred style of .alert and .actionSheet.

// Convenience initializer from UIAlertController+Convenience.swift
let alert = UIAlertController(title: "title",
                              message: "message",
                              preferredStyle: .alert,
                              alertActions: [
                                  .okay({ _ in
                                      // Okay Pressed
                                  }),
                                  .cancel({ _ in
                                      // Cancel Pressed
                                  }),
                              ])
])

alert.show() // Shows the alert

KeyboardNotificationInfo

KeyboardNotificationInfo is a data model for the Notification object that is sent when responding to keyboard events.

Notification Properties:

  • animationDuration (TimeInterval)
  • animationOptions (UIView.AnimationOptions?)
  • beginFrame (CGRect)
  • endFrame (CGRect)
  • isLocalUser (Bool)

KeyboardObservable

KeyboardObservable is a protocol that adds functions to register/un-register from keyboard notifications. Additionally, it exposes the functions that are called when those events fire.

class FooViewController: UIViewController, KeyboardObservable {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        registerForKeyboardEvents()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        unregisterForKeyboardEvents()
    }

    func keyboardWillShow(_ notification: Notification) {
        // notification is the Notification object containing the keyboard information
    }

    func keyboardWillHide(_ notification: Notification) {
        // notification is the Notification object containing the keyboard information
    }
}

NOTE: Currently only the UIResponder.keyboardWillShowNotification and UIResponder.keyboardWillHideNotification notifications are registered for.

KeyboardRespondable

KeyboardRespondable extends KeyboardObservable slightly, by adding automatic handling of a view’s content inset.

The protocol requires a contentView that conforms to ContentInsetAdjustable whose only requirement is the conformer must have a mutable contentInset property.

KeyboardRespondable will automatically adjust the content insets of the contentView when the keyboard shows and hides based on the amount of overlap of the keyboard and the contentView.

class FooViewController: UIViewController, KeyboardRespondable {
    @IBOutlet private var contentView: UIScrollView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        registerForKeyboardEvents()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        unregisterForKeyboardEvents()
    }
}

NOTE: If the `keyboardWillShow(:)and/orkeyboardWillHide(:)` functions are added in the code implementation, they will shadow the default implementation and the scroll view will not automatically adjust its insets correctly.

Latest podspec

{
    "name": "iOS-Utils",
    "version": "1.4.0",
    "summary": "A set of convenient iOS utilities",
    "homepage": "https://github.com/ark-develop/iOS-Utils",
    "license": {
        "type": "MIT",
        "file": "LICENSE.md"
    },
    "authors": "ark-develop",
    "source": {
        "git": "https://github.com/ark-develop/iOS-Utils.git",
        "tag": "1.4.0"
    },
    "platforms": {
        "ios": "11.0"
    },
    "swift_version": "5.0",
    "source_files": "Source/**/*.swift",
    "subspecs": [
        {
            "name": "Designable",
            "source_files": [
                "Source/DesignableView.swift",
                "Source/DesignableButton.swift"
            ]
        },
        {
            "name": "AlertController",
            "source_files": [
                "Source/UIAlertController+Convenience.swift",
                "Source/UIAlertController+UIWindow.swift"
            ]
        },
        {
            "name": "Keyboard",
            "source_files": [
                "Source/KeyboardNotificationInfo.swift",
                "Source/KeyboardObservable.swift",
                "Source/KeyboardRespondable.swift"
            ]
        }
    ]
}

Pin It on Pinterest

Share This