Swift3にて、コード記述して表示画面を縦にスクロールさせたいのですがスクロールさせられません。
scrollViewを使ってコードで画面を縦にスクロールする方法を教えて頂きたいです。
宜しくお願い致します。
下記のコードですと、画面の色はyellowになりますが、スクロールもスクロールバーもありません。
■ サンプルコード Swift3 xcode8
Swift3
1class ThirdViewController: UIViewController, UIScrollViewDelegate { 2 3 let q1Label = UILabel() 4 let q2Label = UILabel() 5 let q3Label = UILabel() 6 let q4Label = UILabel() 7 let q5Label = UILabel() 8 let q6Label = UILabel() 9 let q7Label = UILabel() 10 let q8Label = UILabel() 11 let q9Label = UILabel() 12 let q10Label = UILabel() 13 let q11Label = UILabel() 14 let q12Label = UILabel() 15 let q13Label = UILabel() 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 // スクロールビューを生成 21 let scrollView = UIScrollView() 22 scrollView.backgroundColor = UIColor.yellow 23 24 scrollView.frame.size = CGSize(width: view.frame.width, height: view.frame.height) 25 scrollView.center = self.view.center 26 27 scrollView.contentSize = CGSize(width: view.frame.width, height: 800) 28 29 scrollView.bounces = false 30 31 scrollView.indicatorStyle = .white 32 scrollView.scrollIndicatorInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) 33 34 scrollView.delegate = self 35 36 self.view.addSubview(scrollView) 37 38 39 // q1Label作成 40 q1Label.text = qArray[5] 41 q1Label.frame = CGRect(x: 20, y: 230, width: 335, height: 30) 42 q1Label.textColor = UIColor.black 43 q1Label.font = UIFont.systemFont(ofSize: 12) 44 q1Label.backgroundColor = .lightGray 45 scrollView.addSubview(q1Label) 46 ・ 47 ・ 48 (略) 49 ・ 50 ・ 51 52 // q13Label作成 53 let q13Y = q12Label.frame.origin.y + q12Label.frame.size.height 54 q13Label.text = qArray[29] 55 q13Label.frame = CGRect(x: 20, y: q13Y, width: 335, height: 30) 56 q13Label.textColor = UIColor.black 57 q13Label.font = UIFont.systemFont(ofSize: 12) 58 q13Label.backgroundColor = .lightGray 59 scrollView.addSubview(q13Label) 60 } 61} 62
回答1件
あなたの回答
tips
プレビュー