Skip to content

Rayllienstery/TMKeyboardManager

Repository files navigation

TMKeyboardManager


Features

  • Automatic frame for Controllers with keyboard functionality
  • Easiest access to the keyboard behaviour
  • Autohide keyboard on background tap
  • Easy work with multiple InputFields
  • UIPicker/UIDatePicker as keyboard
  • Done and Next+Done toolbar for keyboard

Requirements

  • iOS 14.0+
  • Xcode 13

Installation

Swift Package Manager

Instructions for Swift Package Manager support can be found at SwiftPackageManager Markdown file.

Usage example

Automatic frame for Controllers with keyboard functionality

import UIKit
import TMKeyboardManager

class ViewController: UIViewController, TMKeyboardDelegate {
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.initKeyboard()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.deinitKeyboard()
    }
}

Easiest access to the keyboard behaviour

import UIKit
import TMKeyboardManager

class ViewController: UIViewController, TMKeyboardDelegate {
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.initKeyboard()

    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.deinitKeyboard()
    }

    // MARK: - TMKeyboardDelegate
    func keyboardWillShow() {
        print("keyboardWillShow")
    }

    func keyboardWillHide() {
        print("keyboardWillHide")
    }

    func keyboardDidShow() {
        print("keyboardDidShow")
    }

    func keyboardDidHide() {
        print("keyboardDidHide")
    }
}

Easy work with multiple InputFields

Inherit your UITextFielf with TMTextField

Use next functions to add toolbars

firstTextField.delegate = self
secondTextField.delegate = self
        
firstTextField.setNextDoneToolbar()
secondTextField.setDoneToolbar()

And provide shouldReturn Delegate function from UITextFieldDelegate

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    self.focusOnNextPesponder(textField)
}

Full Implementation

import UIKit
import TMKeyboardManager

class DelegateViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var firstTextField: TMTextField!
    @IBOutlet weak var secondTextField: TMTextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        firstTextField.delegate = self
        secondTextField.delegate = self

        firstTextField.setNextDoneToolbar()
        secondTextField.setDoneToolbar()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.initKeyboard()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.deinitKeyboard()
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.focusOnNextPesponder(textField)
    }
}

Picker Keyboard

  1. Inherit your UITextField from TMTextField
@IBOutlet weak var datePickerTextField: TMTextField!
  1. Add setPickerKeyboard with UIPickerViewDelegate as Delegate
override func viewDidLoad() {
    super.viewDidLoad()
    pickerTextField.setPickerKeyboard(self)
}
  1. And implement UIPickerViewDelegate as usual
extension PickerViewController: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {  }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {  }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {  }
}

DatePicker Keyboard

  1. Inherit your UITextField from TMTextField
@IBOutlet weak var datePickerTextField: TMTextField!
  1. Add setDatePickerKeyboard with UIPickerViewDelegate as Delegate and selector (See Step 3)
override func viewDidLoad() {
    super.viewDidLoad()
    datePickerTextField.setDatePickerKeyboard(self, selector: #selector(dateDidChange(sender:)))
}
  1. Add selector with UIDatePicker as Sender
@objc func dateDidChange(sender: UIDatePicker) {
    print(sender.date.description)
}

Customisation

Use TMKeyboardConfig flags to convigure available options:

  • tintColor
import UIKit
import TMKeyboardManager

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        TMKeyboardConfig.tintColor = .systemMint
        return true
    }
}

License


Package released under the Apache 2.0 license, check the LICENSE file for more info.