我正在尝试构建可在许多应用程序中通用的自定义键盘。我想隐藏“键盘快捷键”栏,但是我很难做到这一点。到目前为止,我在论坛上看到的所有帖子都提到将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 = []
答案 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
}
}
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()
}
}