Skip to content

Instantly share code, notes, and snippets.

@yichizhang
Created May 7, 2016 07:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yichizhang/67f4cded839dd2ff1d4183dd67c5edbc to your computer and use it in GitHub Desktop.
Save yichizhang/67f4cded839dd2ff1d4183dd67c5edbc to your computer and use it in GitHub Desktop.
iOS9 Keyboard extension - Custom height keyboard
//
// KeyboardViewController.swift
// CustomHeightKeyboard
//
// Created by Yichi on 3/10/2015.
// Copyright (c) 2015-present Yichi. All rights reserved.
//
import UIKit
class KeyboardViewController: UIInputViewController
{
@IBOutlet var nextKeyboardButton: UIButton!
var heightConstraint: NSLayoutConstraint!
var nextKeyboardButtonLeftSideConstraint: NSLayoutConstraint!
override func updateViewConstraints()
{
super.updateViewConstraints()
// Add custom view sizing constraints here
if (view.frame.size.width == 0 || view.frame.size.height == 0) {
return
}
setUpHeightConstraint()
}
override func viewDidLoad()
{
super.viewDidLoad()
nextKeyboardButton = UIButton(type: .System)
nextKeyboardButton.setTitle(
NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"),
forState: .Normal)
nextKeyboardButton.sizeToFit()
nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
nextKeyboardButton.addTarget(
self,
action: #selector(advanceToNextInputMode),
forControlEvents: .TouchUpInside)
view.addSubview(nextKeyboardButton)
}
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
// Set up constraints for next keyboard button in view did appear
if nextKeyboardButtonLeftSideConstraint == nil {
nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(
item: nextKeyboardButton,
attribute: .Left,
relatedBy: .Equal,
toItem: view,
attribute: .Left,
multiplier: 1.0,
constant: 0.0)
let nextKeyboardButtonBottomConstraint = NSLayoutConstraint(
item: nextKeyboardButton,
attribute: .Bottom,
relatedBy: .Equal,
toItem: view,
attribute: .Bottom,
multiplier: 1.0,
constant: 0.0)
view.addConstraints([
nextKeyboardButtonLeftSideConstraint,
nextKeyboardButtonBottomConstraint])
}
}
// MARK: Set up height constraint
func setUpHeightConstraint()
{
let customHeight = UIScreen.mainScreen().bounds.height / 2
if heightConstraint == nil {
heightConstraint = NSLayoutConstraint(item: view,
attribute: .Height,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 1,
constant: customHeight)
heightConstraint.priority = UILayoutPriority(UILayoutPriorityRequired)
view.addConstraint(heightConstraint)
}
else {
heightConstraint.constant = customHeight
}
}
// MARK: Text related
override func textWillChange(textInput: UITextInput?)
{
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(textInput: UITextInput?)
{
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
let proxy = textDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
textColor = UIColor.whiteColor()
}
else {
textColor = UIColor.blackColor()
}
nextKeyboardButton.setTitleColor(textColor, forState: .Normal)
}
}
@yichizhang
Copy link
Author

Full source code for the project: https://github.com/yichizhang/CustomHeightKeyboard.git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment