UIScrollView Swift - 添加图像

时间:2018-06-15 12:17:07

标签: ios swift xcode uiscrollview

我试图通过将颜色作为图像添加到UIScrollView中来获得简单的颜色选择器。 我之前使用过UIScrollView,从未遇到过任何问题。这一次无论我做什么都没有显示出来。双重检查颜色是否正确,一切似乎都很好。由于某种原因,颜色不会添加到UIScrollView

func fillColorPicker(colors: Array<String>){
    for i in 0..<colors.count{
        let imageView = UIImageView()
        imageView.image = UIImage(named:     colors[i])
        imageView.contentMode = .scaleAspectFit

        let xPosition = CGFloat(i) * imageView.frame.width
        imageView.frame = CGRect(x: xPosition, y: 0, width: imageView.frame.width, height: self.colorPicker.frame.height)
        colorPicker.contentSize.width = imageView.frame.width * CGFloat(i+1)
        colorPicker.addSubview(imageView)
    }
}

找到了一些东西。不知怎的,imageView.frame.width始终为0.我假设它会占用图像文件的宽度?

解决方案:

func fillColorPicker(colors: Array<String>){
    for i in 0..<colors.count{
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 75, height: 75))
        imageView.image = UIImage(named: colors[i])
        imageView.contentMode = .scaleAspectFit


        let xPosition = CGFloat(i) * imageView.frame.width
        imageView.frame = CGRect(x: xPosition, y: 0, width: imageView.frame.width, height: self.colorPicker.frame.height)
        colorPicker.contentSize.width = imageView.frame.width * CGFloat(i+1)
        colorPicker.addSubview(imageView)
    }
}

0 个答案:

没有答案