ios 将 UIToolBar 添加到所有键盘 (swift)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30983516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add UIToolBar to all keyboards (swift)
提问by Vivian
I'm trying to add a custom UIToolBar to all of my keyboards with as little repetition. The way I'm currently doing it requires me to add the code to all my viewDidLoads and assign every textfield's delegate to the viewController I'm using. I have tried creating my own UIToolBar subclass but I find that I can't really do that when the target for my "Done" and "cancel" buttons are the self view. Does anyone have any suggestions for creating an easily reusable toolbar? Thanks in advance.
我正在尝试将自定义 UIToolBar 添加到我所有的键盘中,并且重复很少。我目前这样做的方式要求我将代码添加到我的所有 viewDidLoads 并将每个文本字段的委托分配给我正在使用的 viewController。我尝试创建自己的 UIToolBar 子类,但我发现当我的“完成”和“取消”按钮的目标是自我视图时,我无法真正做到这一点。有没有人对创建易于重用的工具栏有任何建议?提前致谢。
override func viewDidLoad() {
super.viewDidLoad()
var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "donePressed")
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelPressed")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
toolBar.sizeToFit()
stateField.inputAccessoryView = toolBar
stateField.delegate = self
回答by Vivian
Thanks to Glorfindel's suggestion and ncerezo's sample code I solved my problem using extensions.
感谢 Glorfindel 的建议和 ncerezo 的示例代码,我使用扩展解决了我的问题。
extension UIViewController: UITextFieldDelegate{
func addToolBar(textField: UITextField){
var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "donePressed")
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelPressed")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
toolBar.sizeToFit()
textField.delegate = self
textField.inputAccessoryView = toolBar
}
func donePressed(){
view.endEditing(true)
}
func cancelPressed(){
view.endEditing(true) // or do something
}
}
Though I still need to call the code below on all of my textfields. I feel like there may be a better way without having to call the function on every textField, but for now this is definitely more reusable.
虽然我仍然需要在我的所有文本字段上调用下面的代码。我觉得可能有更好的方法而不必在每个 textField 上调用函数,但现在这绝对是更可重用的。
override func viewDidLoad() {
super.viewDidLoad()
addToolBar(addressField)
}
回答by alessioarsuffi
equivalent of vivian version in swift 3:
相当于 swift 3 中的 vivian 版本:
extension UIViewController: UITextFieldDelegate {
func addToolBar(textField: UITextField) {
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76 / 255, green: 217 / 255, blue: 100 / 255, alpha: 1)
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(donePressed))
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelPressed))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
textField.delegate = self
textField.inputAccessoryView = toolBar
}
func donePressed() {
view.endEditing(true)
}
func cancelPressed() {
view.endEditing(true) // or do something
}
}
回答by Deepak Kumar
For swift 4 you can use this:-
对于 swift 4,你可以使用这个:-
extension UIViewController : UITextFieldDelegate {
func addToolBar(textField: UITextField){
var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: "donePressed")
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: "cancelPressed")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
textField.delegate = self
textField.inputAccessoryView = toolBar
}
func donePressed(){
view.endEditing(true)
}
func cancelPressed(){
view.endEditing(true) // or do something
}
}
Use
用
override func viewDidLoad() {
super.viewDidLoad()
addToolBar(addressField)
}
回答by Aaron Frary
You can make a UIToolbar subclass work by taking advantage of the responder chain. No need to change your view controllers. In Swift 3:
您可以利用响应者链使 UIToolbar 子类工作。无需更改您的视图控制器。在 Swift 3 中:
class KeyboardAccessoryToolbar: UIToolbar {
convenience init() {
self.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
self.barStyle = .default
self.isTranslucent = true
self.tintColor = UIColor(red: 76 / 255, green: 217 / 255, blue: 100 / 255, alpha: 1)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.done))
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(self.cancel))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
self.items = [cancelButton, spaceButton, doneButton]
self.isUserInteractionEnabled = true
self.sizeToFit()
}
func done() {
// Tell the current first responder (the current text input) to resign.
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
func cancel() {
// Call "cancel" method on first object in the responder chain that implements it.
UIApplication.shared.sendAction(#selector(cancel), to: nil, from: nil, for: nil)
}
}
Then add the following to your applicationDidFinishLaunching
to apply this to all your keyboards:
然后将以下内容添加到您的applicationDidFinishLaunching
以将其应用于所有键盘:
let accessoryView = KeyboardAccessoryToolbar()
UITextField.appearance().inputAccessoryView = accessoryView
UITextView.appearance().inputAccessoryView = accessoryView
回答by ncerezo
I have an utility that I've been using some time (ported to Swift from Objective-C) that does this and a little more, you might find it useful:
我有一个实用程序,我一直在使用它(从 Objective-C 移植到 Swift),它可以做到这一点以及更多,您可能会发现它很有用:
https://github.com/ncerezo/SwiftKeyboardAccessory
https://github.com/ncerezo/SwiftKeyboardAccessory
It creates a toolbar with at least a "Done" button to dismiss the keyboard, and optionally Next and Previous buttons. It also takes care of dismissing the keyboard when you tap outside the text field, and of resizing and scrolling the view when the keyboard appears or disappears. It's designed to work with UITableVIew and also with UIScrollView.
它创建一个工具栏,至少有一个“完成”按钮来关闭键盘,以及可选的下一个和上一个按钮。它还负责在您点击文本字段外时关闭键盘,以及在键盘出现或消失时调整视图大小和滚动视图。它旨在与 UITableVIew 和 UIScrollView 一起使用。