質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

371閲覧

TextFieldで画面サイズにあったキーボード表示を行いたいです。

退会済みユーザー

退会済みユーザー

総合スコア0

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/12/12 09:15

勉強不足ですが、ご意見いただけたら嬉しいです。
クイズの回答画面を作成中で、TextFieldを入れることができたのですが、
Assistant editorで実行すると、画面の枠を大きく超えてキーボードが表示され
打つことができません。
Keyboardのタイプを変更したりしてもダメでした。
どのように解決し行けば良いでしょうか。
教えていただきたいです。

##対象コード
swiftclass ViewController_11:UIViewController, UITextFieldDelegate {

override func loadView() { let view = UIView() view.frame = CGRect(x:0, y:0, width:375, height:669) view.backgroundColor = .white self.view = view let label = UILabel() label.frame = CGRect(x:20, y:40, width:200, height:60) label.text = "問" label.textColor = .black label.font = UIFont.systemFont(ofSize:35) view.addSubview(label) let textView1 = UITextView() textView1.frame = CGRect(x:20, y:100, width:360, height:100) textView1.text = "1 今日は何年何月何日ですか。" textView1.textColor = .black textView1.font = UIFont.systemFont(ofSize:25) view.addSubview(textView1) let textView2 = UITextView() textView2.frame = CGRect(x:20, y:220, width:350, height:100) textView2.text = "2 今日は何曜日ですか。" textView2.textColor = .black textView2.font = UIFont.systemFont(ofSize:25) view.addSubview(textView2) let textView3 = UITextView() textView3.frame = CGRect(x:20, y:340, width:360, height:100) textView3.text = "3 今の季節を教えてください。" textView3.textColor = .black textView3.font = UIFont.systemFont(ofSize:25) view.addSubview(textView3) let textView4 = UITextView() textView4.frame = CGRect(x:20, y:460, width:350, height:100) textView4.text = "4 現在の首相は誰ですか。" textView4.textColor = .black textView4.font = UIFont.systemFont(ofSize:25) view.addSubview(textView4) let yearL = UILabel() yearL.frame = CGRect(x:132, y:160, width:30, height:30) yearL.text = "年" yearL.textColor = .black yearL.font = UIFont.systemFont(ofSize:25) view.addSubview(yearL) let monthL = UILabel() monthL.frame = CGRect(x:220, y:160, width:30, height:30) monthL.text = "月" monthL.textColor = .black monthL.font = UIFont.systemFont(ofSize:25) view.addSubview(monthL) let dayL = UILabel() dayL.frame = CGRect(x:310, y:160, width:30, height:30) dayL.text = "日" dayL.textColor = .black dayL.font = UIFont.systemFont(ofSize:25) view.addSubview(dayL) let weekL = UILabel() weekL.frame = CGRect(x:210, y:280, width:100, height:30) weekL.text = "曜日" weekL.textColor = .black weekL.font = UIFont.systemFont(ofSize:30) view.addSubview(weekL) let button11 = UIButton() button11.frame = CGRect(x:230, y:600, width:130, height:50) button11.backgroundColor = .blue button11.setTitle("次へ", for: .normal) button11.setTitleColor(UIColor.white, for: .highlighted) button11.layer.cornerRadius = 20.0 button11.addTarget(self, action:#selector(ViewController_11.tapped_touch(_:)), for: .touchUpInside) view.addSubview(button11) } override func viewDidLoad(){ super.viewDidLoad() let textF1 = UITextField() textF1.frame = CGRect(x:30, y:160, width:100, height:30) textF1.delegate = self textF1.borderStyle = .roundedRect textF1.returnKeyType = .done textF1.placeholder = "入力" textF1.keyboardType = UIKeyboardType.numbersAndPunctuation self.view.addSubview(textF1) let textF2 = UITextField() textF2.frame = CGRect(x:160, y:160, width:60, height:30) textF2.delegate = self textF2.borderStyle = .roundedRect textF2.returnKeyType = .done textF2.placeholder = "入力" textF2.keyboardType = UIKeyboardType.numbersAndPunctuation self.view.addSubview(textF2) let textF3 = UITextField() textF3.frame = CGRect(x:250, y:160, width:60, height:30) textF3.delegate = self textF3.borderStyle = .roundedRect textF3.returnKeyType = .done textF3.placeholder = "入力" textF3.keyboardType = UIKeyboardType.numbersAndPunctuation self.view.addSubview(textF3) let textF4 = UITextField() textF4.frame = CGRect(x:90, y:270, width: 100, height:50) textF4.delegate = self textF4.borderStyle = .roundedRect textF4.returnKeyType = .done textF4.placeholder = "入力" textF4.keyboardType = UIKeyboardType.asciiCapable self.view.addSubview(textF4) let textF5 = UITextField() textF5.frame = CGRect(x:90, y:390, width: 100, height:50) textF5.delegate = self textF5.borderStyle = .roundedRect textF5.returnKeyType = .done textF5.placeholder = "入力" textF5.keyboardType = UIKeyboardType.asciiCapable self.view.addSubview(textF5) let textF6 = UITextField() textF6.frame = CGRect(x:90, y:500, width: 200, height:50) textF6.delegate = self textF6.borderStyle = .roundedRect textF6.returnKeyType = .done textF6.placeholder = "入力" textF6.keyboardType = UIKeyboardType.asciiCapable self.view.addSubview(textF6) } @objc func tapped_touch(_ sender:UIButton) { let nextvc = ViewController_11() self.present(nextvc, animated:true, completion:nil) }

}
PlaygroundPage.current.liveView = ViewController_11()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

最初、「Assistant editorで実行」の意味がわかりませんでしたが、
これはPlaygroundでUIKitを使うプログラムを実行した時の質問なのですね。

以下の参考URLに同じ質問とその回答がありますが、Playgroundで実行する時の
スクリーンサイズは768x1024固定(つまりiPadサイズ)になっているので
キーボードサイズは横768ptから変えられないそうです。
逆に、今作っているViewControllerのviewのサイズを
768x1024に増やしてしまう手はあるとのことです。

(参考URL)
https://stackoverflow.com/questions/46867712/swift-playground-uitextfield-spawns-keyboard-that-is-too-big

どうしてここまでPlaygroundで頑張ろうとしているのかわかっていませんが、
iPhoneの画面サイズでユーザーとの対話処理を実行したいのであれば、
プロジェクトを作って、シミュレータで実行した方が苦労が少ないと思います。

投稿2018/12/12 16:20

TakeOne

総合スコア6299

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/12/13 02:53

回答いただきありがとうございます。また、説明不足だったことすみませんでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問