MetovaBase 2.0.0

MetovaBase 2.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2018
SPMSupports SPM

Maintained by Logan Gauthier, Nick Griffith, Chris Dillard, Chris Martin, Beverly Massengill, Metova, Inc..



  • By
  • Nick Griffith and Logan Gauthier

MetovaBase

Build Status CocoaPods Compatible Documentation Coverage Status Platform Twitter

Requirements

  • iOS 8.0
  • Swift 4.2

Installation

MetovaBase is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'MetovaBase'

Overview

MetovaBase offers useful UIKit subclasses that implement commonly used utilities.

BaseViewController

The BaseViewController class extends from UIViewController and adds a common utility that we have found ourselves adding in most, if not all, of our view controller classes. It does some additional setup in viewDidLoad to provide your view controller with keyboard notifications. If overriding, be sure to call super.viewDidLoad in your subclass.

Example usage:

import MetovaBase

class MyViewController: BaseViewController {

    override func viewDidLoad() {
    
    	// this call registers your VC for keyboard notifications
    	super.viewDidLoad() 
    	
    	// my custom setup here
    }
}

Additionally, BaseViewController unregisters your view controller from receiving keyboard notifications in its deinit() method.

KeyboardNotificationDelegate

This protocol contains a collection of methods which BaseViewController subclasses (or extensions) can implement in order to hear about keyboard notifications. As already stated, you do not need to register or unregister for these notifications in your subclass or extension--you simply need to implement these methods.

Methods

These methods are called when the appropriate keyboard notification is fired.

@objc optional func keyboardWillShow(notification: Notification)

@objc optional func keyboardDidShow(notification: Notification)

@objc optional func keyboardWillChangeFrame(notification: Notification)

@objc optional func keyboardDidChangeFrame(notification: Notification)

@objc optional func keyboardWillHide(notification: Notification)

@objc optional func keyboardDidHide(notification: Notification)

BaseViewControllerConvenience

BaseViewControllerConvenience provides a convenience method to dismiss the keyboard. It assumes the first responder is a descendant of your view controller's view. Since it is an extension of BaseViewController, you can call the method anywhere in your BaseViewController subclass like so:

dismissKeyboard()

Or as a selector like so:

dismissGestureRecognizer.addTarget(self, action: #selector(dismissKeyboard))

BaseViewControllerFrames

BaseViewControllerFrames provides some additional helper methods for handling the keyboard in your view controller. Again, this extends BaseViewController, so these methods are available anywhere in your BaseViewController subclass.

Methods

public func framesForKeyboard(notification: Notification) -> (fromFrame: CGRect, toFrame: CGRect) 

This method returns the starting and ending frames of the keyboard. It parses the frame information from the notification that is passed in. If there is no frame information in the notification object, CGRect.zero is returned.

public func adjustContentInset(scrollview scrollView: UIScrollView, forKeyboardWillChangeFrameNotification notification: Notification)

As can be surmised by the name, this method adjusts the scrollview's inset based on the keyboard frame. This is helpful for views where the keyboard may cover up the first responder.

Example Usage

import MetovaBase

class MyViewController: BaseViewController, KeyboardNotificationDelegate {

	@IBOutlet myScrollView: UIScrollView!

	@objc func keyboardDidChangeFrame(notification: Notification) {
	
		let frames = framesForKeyboard(notification: notification)
		let fromFrame = frames.fromFrame
		let toFrame = frames.toFrame
		
		// do whatever you need to with the 'fromFrame' and 'toFrame'
	}

	@objc func keyboardWillChangeFrame(notification: Notification) {

		adjustContentInset(scrollView: myScrollView, forKeyboardWillChangeNotification: notification)
	}
}

Credits

MetovaBase is owned and maintained by Metova Inc.

Contributors

If you would like to contribute to Metova Test Kit, see our CONTRIBUTING guidelines.

Metova Test Kit banner image and other assets provided by Christi Johnson.

License

MetovaBase is available under the MIT license. See the LICENSE file for more info.