###前提・実現したいこと
Qiitaの記事UIAlertにUIPickerViewを表示する(Swift2.1)を参考に、UIPickerViewとUILabelを含んだUIAlertを簡易版ではありますが作成しました。
Simulatorで確認したところ、iPhone7ではUIAlertが理想通りに表示されるものの、iPad Pro (9.7
inch)で実行すると画面サイズの影響からかUIPickerViewとUILabelの位置がずれUIAlertからはみ出してしまいました。配置やサイズを固定しているのが原因ということは分かっているのですが、改善方法が分かりません。
完成図の理想としては、以下のような配置にしたいと考えています。
※ UIPickerViewがUIAlertに収まる。
※ UILabelがUIPickerViewの下に配置され、かつUIAlertの対して中央揃えになるよう配置される。
(※ iPhone7の図を理想とする)
この問題に関して教えていただければ幸いです。よろしくお願いいたします。
###実行結果
###該当のソースコード
Swift
1import UIKit 2 3class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{ 4 5 let sex: [String] = ["男性", "女性"] 6 let age: [String] = ["0~10", "11~20", "21~30","31~"] 7 var selectSex: String? 8 var selectAge: String? 9 var isSelect: String? 10 11 let pv = UIPickerView() 12 let Label = UILabel() 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 selectSex = sex[0] 17 selectAge = age[0] 18 textView() 19 textReload() 20 } 21 22 override func viewDidAppear(_ animated: Bool) { 23 24 let title = "Title" 25 let message = "Message\n\n\n\n\n\n\n\n\n\n" //改行入れないとOKCancelがかぶる 26 27 let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) 28 let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:{ 29 (action: UIAlertAction!) -> Void in 30 }) 31 32 // PickerView 33 pv.selectRow(0, inComponent: 0, animated: true) // 初期値 34 pv.frame = CGRect(x: 0, y: 60, width: view.bounds.width * 0.7, height: 150) // 配置、サイズ 35 pv.dataSource = self 36 pv.delegate = self 37 alert.view.addSubview(pv) 38 39 Label.frame = CGRect(x: 40, y: 200, width: view.bounds.width * 0.8, height: 30) 40 alert.view.addSubview(Label) 41 42 alert.addAction(okAction) 43 present(alert, animated: true, completion: nil) 44 } 45 46 func numberOfComponents(in pickerView: UIPickerView) -> Int { 47 return 2 48 } 49 50 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 51 if component == 0 { 52 return sex.count 53 } else if component == 1 { 54 return age.count 55 } 56 return 0 57 } 58 59 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 60 if component == 0 { 61 return sex[row] 62 } else if component == 1 { 63 return age[row] 64 } 65 return "" 66 } 67 68 69 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 70 if component == 0 { 71 selectSex = sex[row] 72 }else if component == 1 { 73 selectAge = age[row] 74 } 75 self.textView() 76 self.textReload() 77 } 78 79 func textReload(){ 80 if let isSelect = self.isSelect { 81 self.Label.text = ("Text:\(String(describing: isSelect))") 82 } 83 } 84 85 func textView(){ 86 if selectSex == sex[0] { 87 if selectAge == age[0] { 88 isSelect = "AA" 89 } else if selectAge == age[1] { 90 isSelect = "AB" 91 } else if selectAge == age[2] { 92 isSelect = "AC" 93 } else if selectAge == age[3] { 94 isSelect = "AD" 95 } 96 } else if selectSex == sex[1] { 97 if selectAge == age[0] { 98 isSelect = "BA" 99 } else if selectAge == age[1] { 100 isSelect = "BB" 101 } else if selectAge == age[2] { 102 isSelect = "BC" 103 } else if selectAge == age[3] { 104 isSelect = "BD" 105 } 106 } else { 107 print("エラー") 108 } 109 } 110 111} 112
###補足情報(言語/FW/ツール等のバージョンなど)
swift 3.0 / Xcode 8.3.3 を使用しています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/23 08:27