Skip to main content
コードブロックの範囲を修正しました
ソース リンク
supa
  • 5,399
  • 4
  • 18
  • 41
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        self.navigationController?.setNavigationBarHidden(true, animated: false)

        var tokyoCount = 0
        var tokyoArray = [0]

        // CSV読み込み
        if let csvPath = Bundle.main.path(forResource: "mise", ofType: "csv") {
            do {
                var csvString =  try NSString(contentsOfFile: csvPath, encoding: String.Encoding.utf8.rawValue) as String
                csvString = csvString.replacingOccurrences(of: "\r\n", with: "")
                let array2:Array = csvString.components(separatedBy: ",")

                var items2 = [String]()
                array2.enumerated().forEach { (index, value) in

                    items2.append(value)

                    if value.range(of:string) != nil{
                        print("exists")

                        tokyoArray.append(tokyoCount)
                    }

                    if (index + 1) % 6 == 0 {

                        str2.append(items2)
                        items2 = [String]()

                        tokyoCount += 1

                    }

                }
            } catch {
                // エラー
            }
        }


        // ScrollViewを生成.
        myScrollView = UIScrollView()
        myScrollView.frame = CGRect(x: 10, y: 50, width: 300, height: self.view.frame.size.height)
        let myImageView = UIImageView()
        myImageView.frame = myScrollView.frame
        myImageView.contentMode = UIViewContentMode.scaleAspectFill
        myScrollView.addSubview(myImageView)
        myScrollView.backgroundColor = UIColor.gray
        myScrollView.alpha = 0.3

        // Labelを作成.
        let label: UILabel = UILabel(frame: CGRect(x: 20, y: 10, width: 200, height: 30))
        label.backgroundColor = UIColor.white
        label.layer.masksToBounds = true
        label.textColor = UIColor.black
        label.text = "店舗一覧"
        label.textAlignment = NSTextAlignment.center
        self.view.addSubview(label)

        let v:UIView = UIView(frame: CGRect(x: 10, y: 0, width: 300, height: 50))
        v.backgroundColor = UIColor(red: 0.5, green: 1.0, blue: 0.3, alpha: 1.0);

        for var i in 1...tokyoArray.count-1{

            //店名
            let nameLabel: UILabel = UILabel(frame: CGRect(x: 20, y: 10+(i*100), width: 250, height: 50))
            nameLabel.backgroundColor = UIColor.white
            nameLabel.numberOfLines = 0
            nameLabel.textColor = UIColor.black
            nameLabel.tag = i
            nameLabel.text = str2[tokyoArray[i]][0]
            nameLabel.textAlignment = NSTextAlignment.left
            myScrollView.addSubview(nameLabel)

            //住所
            let addressLabel: UILabel = UILabel(frame: CGRect(x: 20, y: 50+(i*100), width: 250, height: 50))
            addressLabel.backgroundColor = UIColor.white
            addressLabel.numberOfLines = 0
            addressLabel.textColor = UIColor.black
            addressLabel.tag = i
            addressLabel.text = str2[tokyoArray[i]][1]
            addressLabel.textAlignment = NSTextAlignment.left
            myScrollView.addSubview(addressLabel)


        }

        v.sizeToFit()
        myScrollView.sizeToFit()
        myScrollView.contentSize = CGSize(width:300, height:myScrollView.frame.size.height)
        self.view.addSubview(myScrollView) 

   }

}

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.white

    self.navigationController?.setNavigationBarHidden(true, animated: false)

    var tokyoCount = 0
    var tokyoArray = [0]

    // CSV読み込み
    if let csvPath = Bundle.main.path(forResource: "mise", ofType: "csv") {
        do {
            var csvString =  try NSString(contentsOfFile: csvPath, encoding: String.Encoding.utf8.rawValue) as String
            csvString = csvString.replacingOccurrences(of: "\r\n", with: "")
            let array2:Array = csvString.components(separatedBy: ",")

            var items2 = [String]()
            array2.enumerated().forEach { (index, value) in

                items2.append(value)

                if value.range(of:string) != nil{
                    print("exists")

                    tokyoArray.append(tokyoCount)
                }

                if (index + 1) % 6 == 0 {

                    str2.append(items2)
                    items2 = [String]()

                    tokyoCount += 1

                }

            }
        } catch {
            // エラー
        }
    }


    // ScrollViewを生成.
    myScrollView = UIScrollView()
    myScrollView.frame = CGRect(x: 10, y: 50, width: 300, height: self.view.frame.size.height)
    let myImageView = UIImageView()
    myImageView.frame = myScrollView.frame
    myImageView.contentMode = UIViewContentMode.scaleAspectFill
    myScrollView.addSubview(myImageView)
    myScrollView.backgroundColor = UIColor.gray
    myScrollView.alpha = 0.3

    // Labelを作成.
    let label: UILabel = UILabel(frame: CGRect(x: 20, y: 10, width: 200, height: 30))
    label.backgroundColor = UIColor.white
    label.layer.masksToBounds = true
    label.textColor = UIColor.black
    label.text = "店舗一覧"
    label.textAlignment = NSTextAlignment.center
    self.view.addSubview(label)

    let v:UIView = UIView(frame: CGRect(x: 10, y: 0, width: 300, height: 50))
    v.backgroundColor = UIColor(red: 0.5, green: 1.0, blue: 0.3, alpha: 1.0);

    for var i in 1...tokyoArray.count-1{

        //店名
        let nameLabel: UILabel = UILabel(frame: CGRect(x: 20, y: 10+(i*100), width: 250, height: 50))
        nameLabel.backgroundColor = UIColor.white
        nameLabel.numberOfLines = 0
        nameLabel.textColor = UIColor.black
        nameLabel.tag = i
        nameLabel.text = str2[tokyoArray[i]][0]
        nameLabel.textAlignment = NSTextAlignment.left
        myScrollView.addSubview(nameLabel)

        //住所
        let addressLabel: UILabel = UILabel(frame: CGRect(x: 20, y: 50+(i*100), width: 250, height: 50))
        addressLabel.backgroundColor = UIColor.white
        addressLabel.numberOfLines = 0
        addressLabel.textColor = UIColor.black
        addressLabel.tag = i
        addressLabel.text = str2[tokyoArray[i]][1]
        addressLabel.textAlignment = NSTextAlignment.left
        myScrollView.addSubview(addressLabel)


    }

    v.sizeToFit()
    myScrollView.sizeToFit()
    myScrollView.contentSize = CGSize(width:300, height:myScrollView.frame.size.height)
    self.view.addSubview(myScrollView)

}

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        self.navigationController?.setNavigationBarHidden(true, animated: false)

        var tokyoCount = 0
        var tokyoArray = [0]

        // CSV読み込み
        if let csvPath = Bundle.main.path(forResource: "mise", ofType: "csv") {
            do {
                var csvString =  try NSString(contentsOfFile: csvPath, encoding: String.Encoding.utf8.rawValue) as String
                csvString = csvString.replacingOccurrences(of: "\r\n", with: "")
                let array2:Array = csvString.components(separatedBy: ",")

                var items2 = [String]()
                array2.enumerated().forEach { (index, value) in

                    items2.append(value)

                    if value.range(of:string) != nil{
                        print("exists")

                        tokyoArray.append(tokyoCount)
                    }

                    if (index + 1) % 6 == 0 {

                        str2.append(items2)
                        items2 = [String]()

                        tokyoCount += 1

                    }

                }
            } catch {
                // エラー
            }
        }


        // ScrollViewを生成.
        myScrollView = UIScrollView()
        myScrollView.frame = CGRect(x: 10, y: 50, width: 300, height: self.view.frame.size.height)
        let myImageView = UIImageView()
        myImageView.frame = myScrollView.frame
        myImageView.contentMode = UIViewContentMode.scaleAspectFill
        myScrollView.addSubview(myImageView)
        myScrollView.backgroundColor = UIColor.gray
        myScrollView.alpha = 0.3

        // Labelを作成.
        let label: UILabel = UILabel(frame: CGRect(x: 20, y: 10, width: 200, height: 30))
        label.backgroundColor = UIColor.white
        label.layer.masksToBounds = true
        label.textColor = UIColor.black
        label.text = "店舗一覧"
        label.textAlignment = NSTextAlignment.center
        self.view.addSubview(label)

        let v:UIView = UIView(frame: CGRect(x: 10, y: 0, width: 300, height: 50))
        v.backgroundColor = UIColor(red: 0.5, green: 1.0, blue: 0.3, alpha: 1.0);

        for var i in 1...tokyoArray.count-1{

            //店名
            let nameLabel: UILabel = UILabel(frame: CGRect(x: 20, y: 10+(i*100), width: 250, height: 50))
            nameLabel.backgroundColor = UIColor.white
            nameLabel.numberOfLines = 0
            nameLabel.textColor = UIColor.black
            nameLabel.tag = i
            nameLabel.text = str2[tokyoArray[i]][0]
            nameLabel.textAlignment = NSTextAlignment.left
            myScrollView.addSubview(nameLabel)

            //住所
            let addressLabel: UILabel = UILabel(frame: CGRect(x: 20, y: 50+(i*100), width: 250, height: 50))
            addressLabel.backgroundColor = UIColor.white
            addressLabel.numberOfLines = 0
            addressLabel.textColor = UIColor.black
            addressLabel.tag = i
            addressLabel.text = str2[tokyoArray[i]][1]
            addressLabel.textAlignment = NSTextAlignment.left
            myScrollView.addSubview(addressLabel)


        }

        v.sizeToFit()
        myScrollView.sizeToFit()
        myScrollView.contentSize = CGSize(width:300, height:myScrollView.frame.size.height)
        self.view.addSubview(myScrollView) 

   }
ソース リンク
K.K.D
  • 137
  • 7

UILabelの数に応じてUIView、UIScrollViewの長さを変更する方法は?

UIViewにUILabelを複数個追加し、そのUILabelの個数に応じてUIViewの大きさを変え、さらにUIScrollViewに乗せ、ScrollViewをUIViewの大きさに応じて変えたいです。
また、UILabelの個数が30個を超えた場合は31個目以降を2つ目のUIViewに貼り付けし、
ボタンを押すことでページが切り替わるような仕様にしたいです。

UIViewとUIScrollViewの長さを自動で変える方法に「sizeToFit()」があることはわかるのですが、
今のコードでは可変になりません。
どう書けば良いでしょうか?

よろしくお願い致します。

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.white

    self.navigationController?.setNavigationBarHidden(true, animated: false)

    var tokyoCount = 0
    var tokyoArray = [0]

    // CSV読み込み
    if let csvPath = Bundle.main.path(forResource: "mise", ofType: "csv") {
        do {
            var csvString =  try NSString(contentsOfFile: csvPath, encoding: String.Encoding.utf8.rawValue) as String
            csvString = csvString.replacingOccurrences(of: "\r\n", with: "")
            let array2:Array = csvString.components(separatedBy: ",")

            var items2 = [String]()
            array2.enumerated().forEach { (index, value) in

                items2.append(value)

                if value.range(of:string) != nil{
                    print("exists")

                    tokyoArray.append(tokyoCount)
                }

                if (index + 1) % 6 == 0 {

                    str2.append(items2)
                    items2 = [String]()

                    tokyoCount += 1

                }

            }
        } catch {
            // エラー
        }
    }


    // ScrollViewを生成.
    myScrollView = UIScrollView()
    myScrollView.frame = CGRect(x: 10, y: 50, width: 300, height: self.view.frame.size.height)
    let myImageView = UIImageView()
    myImageView.frame = myScrollView.frame
    myImageView.contentMode = UIViewContentMode.scaleAspectFill
    myScrollView.addSubview(myImageView)
    myScrollView.backgroundColor = UIColor.gray
    myScrollView.alpha = 0.3

    // Labelを作成.
    let label: UILabel = UILabel(frame: CGRect(x: 20, y: 10, width: 200, height: 30))
    label.backgroundColor = UIColor.white
    label.layer.masksToBounds = true
    label.textColor = UIColor.black
    label.text = "店舗一覧"
    label.textAlignment = NSTextAlignment.center
    self.view.addSubview(label)

    let v:UIView = UIView(frame: CGRect(x: 10, y: 0, width: 300, height: 50))
    v.backgroundColor = UIColor(red: 0.5, green: 1.0, blue: 0.3, alpha: 1.0);

    for var i in 1...tokyoArray.count-1{

        //店名
        let nameLabel: UILabel = UILabel(frame: CGRect(x: 20, y: 10+(i*100), width: 250, height: 50))
        nameLabel.backgroundColor = UIColor.white
        nameLabel.numberOfLines = 0
        nameLabel.textColor = UIColor.black
        nameLabel.tag = i
        nameLabel.text = str2[tokyoArray[i]][0]
        nameLabel.textAlignment = NSTextAlignment.left
        myScrollView.addSubview(nameLabel)

        //住所
        let addressLabel: UILabel = UILabel(frame: CGRect(x: 20, y: 50+(i*100), width: 250, height: 50))
        addressLabel.backgroundColor = UIColor.white
        addressLabel.numberOfLines = 0
        addressLabel.textColor = UIColor.black
        addressLabel.tag = i
        addressLabel.text = str2[tokyoArray[i]][1]
        addressLabel.textAlignment = NSTextAlignment.left
        myScrollView.addSubview(addressLabel)


    }

    v.sizeToFit()
    myScrollView.sizeToFit()
    myScrollView.contentSize = CGSize(width:300, height:myScrollView.frame.size.height)
    self.view.addSubview(myScrollView)

}