UITextField 키보드에 UIToolbar 추가
실행 환경
전 자료
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var addressField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
addToolBar(textField: addressField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension UIViewController: UITextFieldDelegate{
func addToolBar(textField: UITextField){
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(UIViewController.donePressed))
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.cancelPressed))
let 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
}
}
구현
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var addressField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
addToolBar(textField: addressField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension UIViewController: UITextFieldDelegate{
func addToolBar(textField: UITextField){
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
let JayZButton = UIBarButtonItem(title: "JayZ", style: UIBarButtonItemStyle.done, target: self, action: #selector(UIViewController.RapGod))
let BeyoncéButton = UIBarButtonItem(title: "Beyoncé", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.JayZwife))
let DrakeButton = UIBarButtonItem(title: "Drake", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.bestIeverHad))
let RihannaButton = UIBarButtonItem(title: "Rihanna", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.DrakeLoveRihanna))
//let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
toolBar.setItems([JayZButton, BeyoncéButton, DrakeButton, RihannaButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
textField.delegate = self
textField.inputAccessoryView = toolBar
}
func RapGod(){
// view.endEditing(true)
print("ラップの神様")
}
func JayZwife(){
// view.endEditing(true)
print("JayZの嫁")
}
func bestIeverHad(){
print("bestIeverHad")
}
func DrakeLoveRihanna(){
print("ドレイクはリアーナが大好き")
}
}
참고
Add UIToolBar to all keyboards (swift)
UITextField 키보드에 UIToolbar 추가
출처
UITextField 키보드에 UIToolbar 추가
Reference
이 문제에 관하여(UITextField 키보드에 UIToolbar 추가), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/rh_/items/53c941bbcbc7a2fd60c2
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var addressField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
addToolBar(textField: addressField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension UIViewController: UITextFieldDelegate{
func addToolBar(textField: UITextField){
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(UIViewController.donePressed))
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.cancelPressed))
let 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
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var addressField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
addToolBar(textField: addressField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension UIViewController: UITextFieldDelegate{
func addToolBar(textField: UITextField){
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
let JayZButton = UIBarButtonItem(title: "JayZ", style: UIBarButtonItemStyle.done, target: self, action: #selector(UIViewController.RapGod))
let BeyoncéButton = UIBarButtonItem(title: "Beyoncé", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.JayZwife))
let DrakeButton = UIBarButtonItem(title: "Drake", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.bestIeverHad))
let RihannaButton = UIBarButtonItem(title: "Rihanna", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.DrakeLoveRihanna))
//let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
toolBar.setItems([JayZButton, BeyoncéButton, DrakeButton, RihannaButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
textField.delegate = self
textField.inputAccessoryView = toolBar
}
func RapGod(){
// view.endEditing(true)
print("ラップの神様")
}
func JayZwife(){
// view.endEditing(true)
print("JayZの嫁")
}
func bestIeverHad(){
print("bestIeverHad")
}
func DrakeLoveRihanna(){
print("ドレイクはリアーナが大好き")
}
}
참고
Add UIToolBar to all keyboards (swift)
UITextField 키보드에 UIToolbar 추가
출처
UITextField 키보드에 UIToolbar 추가
Reference
이 문제에 관하여(UITextField 키보드에 UIToolbar 추가), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/rh_/items/53c941bbcbc7a2fd60c2
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
UITextField 키보드에 UIToolbar 추가
Reference
이 문제에 관하여(UITextField 키보드에 UIToolbar 추가), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/rh_/items/53c941bbcbc7a2fd60c2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)