如何在 viewDidLoad 上的 Swift 中显示没有 UITextField 的标准数字键盘
Posted
技术标签:
【中文标题】如何在 viewDidLoad 上的 Swift 中显示没有 UITextField 的标准数字键盘【英文标题】:How to show the standard number keyboard without a UITextField in Swift on viewDidLoad 【发布时间】:2021-01-31 23:45:41 【问题描述】:我试图在用户访问我的ViewController
时显示标准的数字键盘,并且能够捕获用户的输入以在用户每次输入数字时填充 UI 上的一些圆圈,圆圈问题是我在this question 上显示的问题。
我该怎么做?我已经阅读的所有问题都建议使用UIKeyInput
协议来显示自定义键盘,但这不是我想要做的。其中之一是this one。
我想出的代码,在检查了上面的帖子之后,就是这个,但是经过大量阅读和讨论后,这是为了将上面的 UIView
作为我的自定义键盘,我不确定如何制作没错。
视图控制器
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.
DotsView
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
KeyInputView
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访问它们
【问题讨论】:
【参考方案1】:您可能想让您的 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
然后将ViewController
中的viewDidLoad()
更改为:
override func viewDidLoad()
super.viewDidLoad()
dotsView.becomeFirstResponder()
现在,您不需要额外的KeyInputView
。
【讨论】:
这个可行,我只是在尝试您的解决方案后忘记切换键盘以上是关于如何在 viewDidLoad 上的 Swift 中显示没有 UITextField 的标准数字键盘的主要内容,如果未能解决你的问题,请参考以下文章
Swift:如何使滚动视图执行子视图控制器 viewDidLoad
在 UITableView 中的 viewdidload 上选择 Cells,基于 String Array (Swift)