在iOS自定义键盘上隐藏键盘快捷键栏

时间:2018-07-29 15:40:55

标签: ios swift ios-keyboard-extension

我正在尝试构建可在许多应用程序中通用的自定义键盘。我想隐藏“键盘快捷键”栏,但是我很难做到这一点。到目前为止,我在论坛上看到的所有帖子都提到将inputAssistantItem.leadingBarButtonGroups.removeAll()inputAssistantItem.leadingBarButtonGroups = []与文本字段一起使用。

但是,由于我想一般使用它而不是在自己的应用程序中使用,所以我无法访问文本字段。

我正在尝试将其与如下所示的视图一起使用,但显然无法正常工作。我已经尝试了很多变化,以至于我完全感到困惑。

这是我的代码的片段:

import UIKit

class KeyboardViewController: UIInputViewController {
    @IBOutlet var nextKeyboardButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        let nib = UINib(nibName: "golfview", bundle: nil)
        let objects = nib.instantiate(withOwner: self, options: nil)
        view = objects[0] as! UIView;

        //get rid of top bar of keyboard
        view.inputAssistantItem.leadingBarButtonGroups = []
        view.inputAssistantItem.trailingBarButtonGroups = []

1 个答案:

答案 0 :(得分:0)

为自定义视图创建自定义输入视图

方法:

  • 创建绿色输入视图
  • 创建红色视图
  • 当红色视图成为第一响应者时,绿色视图应显示

代码:

绿色输入视图:

import UIKit

class GreenInputView : UIView {

    //MARK: Initializers

    override init(frame: CGRect) {
        super.init(frame: frame)

        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setup()
    }

    //MARK: Setup

    private func setup() {

        backgroundColor = .green

        heightAnchor.constraint(equalToConstant: 200).isActive = true
    }
}

红色视图:

import UIKit

class RedView : UIView {

    private let greenInputView = GreenInputView()

    //MARK: Initializers

    override init(frame: CGRect) {
        super.init(frame: frame)

        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setup()
    }

    //MARK: Setup

    private func setup() {

        setupBase()
        setupGreenInputView()
    }

    private func setupBase() {

        backgroundColor = .red
    }

    private func setupGreenInputView() {
        greenInputView.translatesAutoresizingMaskIntoConstraints = false
    }

    override var canBecomeFirstResponder: Bool {
        return true
    }

    override var inputView: UIView? {
        return greenInputView
    }
}

ViewController:

import UIKit

class ViewController: UIViewController {

    private let redView = RedView()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupRedView()
    }

    private func setupRedView() {

        view.addSubview(redView)

        redView.translatesAutoresizingMaskIntoConstraints = false

        redView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        redView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        redView.widthAnchor.constraint(equalToConstant: 100).isActive = true
        redView.heightAnchor.constraint(equalToConstant: 100).isActive = true
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        redView.becomeFirstResponder()
    }
}

屏幕截图:

Screenshot of green input view