它在默认键盘上工作得很好,但我不能让它在数字键盘上工作.

有什么 idea 吗?

推荐答案

据我所知,你不能在键盘上添加"完成"按钮;你必须在UITextFieldUITextView中加上inputAccessoryView(如果你正在使用的话).

判断documentation for more info.

Edit:判断this question for an example关于如何做到这一点.

Edit 2:差不多example in Swift.

Edit 3:编辑2中的代码,因为链接可能过期.

override func viewDidLoad()
{
    super.viewDidLoad()

    //--- add UIToolBar on keyboard and Done button on UIToolBar ---//
    self.addDoneButtonOnKeyboard()
}

//--- *** ---//

func addDoneButtonOnKeyboard()
{
    var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 50))
    doneToolbar.barStyle = UIBarStyle.BlackTranslucent

    var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("doneButtonAction"))

    var items = NSMutableArray()
    items.addObject(flexSpace)
    items.addObject(done)

    doneToolbar.items = items
    doneToolbar.sizeToFit()

    self.textView.inputAccessoryView = doneToolbar
    self.textField.inputAccessoryView = doneToolbar

}

func doneButtonAction()
{
    self.textViewDescription.resignFirstResponder()
}

Swift 4.2

func addDoneButtonOnKeyboard(){
        let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
        doneToolbar.barStyle = .default

        let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))

        let items = [flexSpace, done]
        doneToolbar.items = items
        doneToolbar.sizeToFit()

        txtMobileNumber.inputAccessoryView = doneToolbar
    }

    @objc func doneButtonAction(){
        txtMobileNumber.resignFirstResponder()
    }

Ios相关问答推荐

如何加密字符串使用AES. GCM在Dart和解密相同的Swift?

Firebase SDK 10+—无法安装软件包

SwiftUI SF Symbols图像:暗模式图像

AVFoundation Camera推出变焦SWIFT

iphone 11 和 iphone 12 对于相同的布局显示不同的结果

Swift Combine:prepend() before share() 阻止初始接收器执行

var name: String 时 print(nil) 和 print(name) 的区别? = 无

在左侧显示多行值时会出现 SwiftUI 错误,这会 destruct 堆栈中右侧的对齐方式

如何在 SwiftUI 中对表格行使用 Transferable

.onChange(of: ) 一次用于多个 @State 属性?

使用 CIImage 支持的 UIImage 设置 UIImageView 时发生罕见的崩溃

如何在通用应用程序中同时支持 iPad 和 iPhone 视网膜图形

iOS 中的 [Class new] 和 [[Class alloc] init] 有什么区别?

try 在 iOS 中处理后退导航按钮操作

swift 语言中的 null / nil

iOS UITextView 或 UILabel 带有可点击的动作链接

文件是为存档而构建的,它不是被链接的体系 struct (i386)

如何删除 Swift 数组中的所有 nil 元素?

UICollectionView:必须用非零布局参数初始化

请在您的 Podfile 中为此目标指定一个平台?