ご覧いただきありがとうございます。
swift
1 2import UIKit 3 4class ViewController: UIViewController , UITextFieldDelegate{ 5 6 // このopenButtonをクリックしたらキーボードを表示させたい 7 @IBOutlet var openButton: UIButton! 8 9 let toolBar: UIView = UIView() 10 let barTextField: UITextField = UITextField() 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 15 // ツールバー 16 toolBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 60) 17 toolBar.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0) 18 toolBar.layer.borderWidth = 0.5 19 toolBar.layer.borderColor = UIColor.lightGray.cgColor 20 21 // テキストフィールドを作成 22 barTextField.frame = CGRect(x: 50, y: 10, width: self.view.frame.width - 65, height: 40) 23 barTextField.placeholder = "コメント..." 24 barTextField.borderStyle = .none 25 barTextField.layer.borderColor = UIColor.lightGray.cgColor 26 barTextField.layer.borderWidth = 1 27 barTextField.layer.masksToBounds = true 28 29 // アイテムをツールバーに追加 30 toolBar.addSubview(barTextField) 31 32 // ツールバーをビューに設定 33 barTextField.inputAccessoryView = toolBar 34 barTextField.delegate = self 35 } 36 37 @IBAction func openKeyboard(_ sender: Any) { 38 // ここでキーボードが出てきてviewDidLoadで生成したToolBarの中のTextFieldにフォーカスがあった状態でキーボードと共に出てくる(と思っていました、、、) 39 barTextField.becomeFirstResponder() 40 } 41 42} 43 44
Swiftを学び始めて日が浅く 「何言ってんだこいつ」 と思われるかもしれませんがご容赦ください。
SNSなどでよくある、ボタンを押すとキーボードと共にTextFieldが出てくるということを行いたいのですが、なぜかキーボードも出てこず、沈黙しております。
試したことは、TextFieldを別に用意して(SwiftでTextFieldを作らずStoryboard上に配置している)、そのTextFieldに対して、inputAccessoryView = toolBar , delegate = self 、 becomeFirstResponder()
すると、キーボードと共にToolBarが出てきて中には動的に生成したTextFieldも存在していました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。