J'essaie d'ajouter un uiview pour être toujours au-dessus du clavier . Je l'ai d'abord fait avec KeyboardWillShow/Hide, mais cela ne couvre pas tous les cas Et j'essaie d'utiliser inputAccesoryView . c'est ce que j'ai essayé:
private var accessoryView = UIView(frame: CGRectZero)
class ViewController : UIViewController {
var myView: customUIView
override var inputAccessoryView: UIView {
return accessoryView
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
accessoryView = myView
}
}
Je reçois le message d'erreur suivant: L'application est arrêtée en raison d'une exception non interceptée 'UIViewControllerHierarchyInconsistency', raison: 'contrôleur de vue enfant: UICompatibilityInputViewController doit avoir le contrôleur de vue parent: MyViewController mais le parent demandé est: UIInputWindowController:'
toute aide serait appréciée!
Pour que la vue reste collée au-dessus du clavier, le code lui-même est assez simple. Le code que vous avez posté n'est pas correct, essayez ceci (notez que vous devez connecter textField
à UITextField
dans votre storyboard):
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 44))
customView.backgroundColor = UIColor.red
textField.inputAccessoryView = customView
}
xcode 9.2, Swift 4
KeyboardToolbarButton
import UIKit
enum KeyboardToolbarButton: Int {
case done = 0
case cancel
case back, backDisabled
case forward, forwardDisabled
func createButton(target: Any?, action: Selector?) -> UIBarButtonItem {
var button: UIBarButtonItem!
switch self {
case .back:
button = UIBarButtonItem(title: "back", style: .plain, target: target, action: action)
case .backDisabled:
button = UIBarButtonItem(title: "back", style: .plain, target: target, action: action)
button.isEnabled = false
case .forward:
button = UIBarButtonItem(title: "forward", style: .plain, target: target, action: action)
case .forwardDisabled:
button = UIBarButtonItem(title: "forward", style: .plain, target: target, action: action)
button.isEnabled = false
case .done:
button = UIBarButtonItem(title: "done", style: .plain, target: target, action: action)
case .cancel:
button = UIBarButtonItem(title: "cancel", style: .plain, target: target, action: action)
}
button.tag = rawValue
return button
}
static func detectType(barButton: UIBarButtonItem) -> KeyboardToolbarButton? {
return KeyboardToolbarButton(rawValue: barButton.tag)
}
}
KeyboardToolbar
import UIKit
protocol KeyboardToolbarDelegate: class {
func keyboardToolbar(button: UIBarButtonItem, type: KeyboardToolbarButton, tappedIn toolbar: KeyboardToolbar)
}
class KeyboardToolbar: UIToolbar {
weak var toolBarDelegate: KeyboardToolbarDelegate?
init() {
super.init(frame: .zero)
barStyle = UIBarStyle.default
isTranslucent = true
sizeToFit()
isUserInteractionEnabled = true
}
func setup(leftButtons: [KeyboardToolbarButton], rightButtons: [KeyboardToolbarButton]) {
let leftBarButtons = leftButtons.map { (item) -> UIBarButtonItem in
return item.createButton(target: self, action: #selector(buttonTapped))
}
let rightBarButtons = rightButtons.map { (item) -> UIBarButtonItem in
return item.createButton(target: self, action: #selector(buttonTapped(sender:)))
}
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
setItems(leftBarButtons + [spaceButton] + rightBarButtons, animated: false)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@objc func buttonTapped(sender: UIBarButtonItem) {
if let type = KeyboardToolbarButton.detectType(barButton: sender) {
toolBarDelegate?.keyboardToolbar(button: sender, type: type, tappedIn: self)
}
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createTextField(frame: CGRect(x: 50, y: 50, width: 200, height: 40), leftButtons: [.backDisabled, .forward], rightButtons: [.cancel])
createTextField(frame: CGRect(x: 50, y: 120, width: 200, height: 40), leftButtons: [.back, .forwardDisabled], rightButtons: [.done])
}
private func createTextField(frame: CGRect, leftButtons: [KeyboardToolbarButton] = [], rightButtons: [KeyboardToolbarButton] = []) {
let textField = UITextField(frame: frame)
textField.borderStyle = .roundedRect
let toolbar = KeyboardToolbar()
toolbar.toolBarDelegate = self
toolbar.setup(leftButtons: leftButtons, rightButtons: rightButtons)
textField.inputAccessoryView = toolbar
view.addSubview(textField)
}
}
extension ViewController: KeyboardToolbarDelegate {
func keyboardToolbar(button: UIBarButtonItem, type: KeyboardToolbarButton, tappedIn toolbar: KeyboardToolbar) {
print("Tapped button type: \(type)")
}
}
Modifications à apporter à votre code:
accessoryView
une hauteurvar myView: customUIView
et le remplacement complet de viewDidLoad()
Essaye ça.
override var inputAccessoryView: UIView? {
get {
return containerView
}
}
override var canBecomeFirstResponder: Bool {
return true
}