gpt4 book ai didi

ios - 隐藏 iOS 自定义键盘上的键盘快捷键栏

转载 作者:行者123 更新时间:2023-11-30 11:17:58 25 4
gpt4 key购买 nike

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

但是,由于我想在一般情况下使用它而不是仅在我自己的应用程序中使用,因此我无法访问文本字段。

我尝试将它与如下所示的 View 一起使用,但显然不起作用。我已经尝试了很多变化,现在我完全困惑了。

这是我的代码片段:

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 = []

最佳答案

为自定义 View 创建自定义输入 View

方法:

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

代码:

绿色输入 View :

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
}
}

红色 View :

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
}
}

View Controller :

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

关于ios - 隐藏 iOS 自定义键盘上的键盘快捷键栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51581924/

25 4 0