Last active
March 26, 2020 02:57
-
-
Save Jimmy-Prime/492fb8013239bb0896bc4ce46398fb73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
private var customInput = false | |
private let textInput = UITextField() | |
private var leadingBarButtonGroups: [UIBarButtonItemGroup] = [] | |
private var trailingBarButtonGroups: [UIBarButtonItemGroup] = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification, object: nil, queue: nil) { notification in | |
print(notification.userInfo!) | |
} | |
view.backgroundColor = .systemBackground | |
textInput.borderStyle = .roundedRect | |
textInput.backgroundColor = .secondarySystemBackground | |
view.addSubview(textInput) | |
textInput.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
textInput.centerXAnchor.constraint(equalTo: view.centerXAnchor), | |
textInput.centerYAnchor.constraint(equalTo: view.centerYAnchor), | |
textInput.widthAnchor.constraint(equalToConstant: 200) | |
]) | |
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toggleInputView)) | |
view.addGestureRecognizer(tapGesture) | |
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(dismissKeyboard)) | |
view.addGestureRecognizer(longPressGesture) | |
} | |
@objc private func dismissKeyboard() { | |
view.endEditing(true) | |
} | |
@objc private func toggleInputView() { | |
guard textInput.isFirstResponder else { return } | |
customInput.toggle() | |
if customInput { | |
let inputView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: 300))) | |
inputView.backgroundColor = .systemRed | |
textInput.inputView = inputView | |
let inputAccessoryView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: 44))) | |
inputAccessoryView.backgroundColor = .systemOrange | |
textInput.inputAccessoryView = inputAccessoryView | |
leadingBarButtonGroups = textInput.inputAssistantItem.leadingBarButtonGroups | |
trailingBarButtonGroups = textInput.inputAssistantItem.trailingBarButtonGroups | |
textInput.inputAssistantItem.leadingBarButtonGroups = [] | |
textInput.inputAssistantItem.trailingBarButtonGroups = [] | |
} else { | |
textInput.inputView = nil | |
textInput.inputAccessoryView = nil | |
textInput.inputAssistantItem.leadingBarButtonGroups = leadingBarButtonGroups | |
textInput.inputAssistantItem.trailingBarButtonGroups = trailingBarButtonGroups | |
} | |
textInput.reloadInputViews() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment