スクロールビューで5ページ分スクロールするアプリを作成しました。
ラベルは5ページ分反映したのですが、画像が反映されません。
実現したいこと
スクロールビューに画像を表示したい
試したこと
下記のコードで警告文が出ており、調べてみたところfor文の中に「i」の記述がされていないため出ているというところまでわかりましたが、その後どのように記述していいかわからず止まりました。
let image = imageArray[i]
警告内容
Initialization of immutable value 'image' was never used; consider replacing with assignment to '_' or removing it
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
var imageArray = ["1","2","3","4","5"]
var tittleArrary = ["砂漠","人","抽象的","笑顔","家族"]
override func viewDidLoad() {
super.viewDidLoad()
//ページングを有効にする
scrollView.isPagingEnabled = true
//5ページ分の幅で、高さはViewの高さに設定する
scrollView.contentSize = CGSize(width: view.frame.size.width*5, height: view.frame.size.height)
for i in 0...4{
print(i)
let imageView = UIImageView()
let image = imageArray[i]
imageView.frame = CGRect(x: CGFloat(i)*view.frame.size.width, y: 0, width: view.frame.size.width, height: view.frame.size.height)
imageView.contentMode = .scaleAspectFit
let label = UILabel(frame: CGRect(x: CGFloat(i)*view.frame.size.width, y: view.frame.size.height - view.frame.size.height/4, width: scrollView.frame.size.width, height: 100))
label.font = UIFont.boldSystemFont(ofSize: 15.0)
label.textAlignment = .center
label.text = tittleArrary[i]
label.textColor = .black
scrollView.addSubview(imageView)
scrollView.addSubview(label)
}
}
}