gpt4 book ai didi

ios - 如何在 viewDidLoad 上的 Swift 中显示没有 UITextField 的标准数字键盘

转载 作者:行者123 更新时间:2023-12-04 08:10:39 25 4
gpt4 key购买 nike

当用户访问我的 ViewController 时,我试图显示标准的数字键盘,并且能够在每次用户输入数字时捕获用户的输入以填充 UI 上的一些圆圈,这些圆圈是我在 this question 上显示的圆圈.
enter image description here
我怎样才能做到这一点?我已经阅读的所有问题都建议使用 UIKeyInput协议(protocol)来显示自定义键盘,但这不是我想要做的。其中之一是this one .
我想出的代码,在检查了上面的帖子之后,就是这个,但是经过大量阅读和讨论,这是为了拥有上面的UIView作为我的自定义键盘,我不确定如何使它正确。

View Controller

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var dotsView: DotsView!

var keyInputView: KeyInputView = KeyInputView()

override func viewDidLoad() {
super.viewDidLoad()

keyInputView.inputView = dotsView
keyInputView.becomeFirstResponder()
//self.view.addSubview(keyInputView) //In the XIB, the view is set to be displayed in center vertically and horizontally. If we uncomment this line, the view is pushed downwards.
}
}
点查看
import UIKit

class DotsView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet var digitDots: [CircleView]!

//initWithFrame to init view from code
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}

//initWithCode to init view from xib or storyboard
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}

//common func to init our view
private func setupView() {
Bundle.main.loadNibNamed("DotsView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
}
环视
import UIKit

@IBDesignable
class CircleView: UIView {
private var shapeLayer = CAShapeLayer()
@IBInspectable var fillColor: UIColor = .blue {
didSet {
shapeLayer.fillColor = fillColor.cgColor
}
}

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

configure()
}

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

configure()
}

private func configure() {
shapeLayer.fillColor = fillColor.cgColor
layer.addSublayer(shapeLayer)
}

override func layoutSubviews() {
super.layoutSubviews()

shapeLayer.path = UIBezierPath(ovalIn: bounds).cgPath
}
}
键盘输入 View
import UIKit

class KeyInputView: UIView {
var _inputView: UIView?

override var canBecomeFirstResponder: Bool { return true }
override var canResignFirstResponder: Bool { return true }

override var inputView: UIView? {
set { _inputView = newValue }
get { return _inputView }
}
}

// MARK: - UIKeyInput
//Modify if need more functionality
extension KeyInputView: UIKeyInput {
var hasText: Bool {
return false
}

func insertText(_ text: String) {

}

func deleteBackward() {

}
}
至于 XIB,您可以在 this repo 中访问它们。

最佳答案

你可能想把你的 DotsView符合 UIKeyInput ...

class DotsView: UIView, UIKeyInput {

@IBOutlet var contentView: UIView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet var digitDots: [CircleView]!

//initWithFrame to init view from code
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}

//initWithCode to init view from xib or storyboard
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}

//common func to init our view
private func setupView() {
Bundle.main.loadNibNamed("DotsView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}

var keyboardType: UIKeyboardType {
get {
return .numberPad
}
set {}
}

// required by UIKeyInput protocol
var hasText: Bool = false

override var canBecomeFirstResponder: Bool {
true
}

func insertText(_ text: String) {
print("Number tapped:", text)
// do something with the number that was tapped
}

func deleteBackward() {
print("Delete Backward tapped")
// do something because Delete was tapped
}
}
然后更改您的 viewDidLoad()ViewController到:
override func viewDidLoad() {
super.viewDidLoad()
dotsView.becomeFirstResponder()
}
现在,您不需要额外的 KeyInputView .

关于ios - 如何在 viewDidLoad 上的 Swift 中显示没有 UITextField 的标准数字键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65985448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com