【问题标题】:Loop to create UIImageView and Labels in Swift循环在 Swift 中创建 UIImageView 和标签
【发布时间】:2021-10-05 06:50:56
【问题描述】:

你好!


我有一个作业,我需要使用一个循环,在屏幕上生成四个 UIImageView 和 16 个标签,如图所示(不需要生成额外的元素)。


所以,总的来说,我想我知道该怎么做,但我有一些问题: 我需要使用 for i in 0 ... 3 循环,我在其中说 var image = UIImageView ()?


var currentY: CGFloat = 0
    let width: CGFloat = 375
    let height: CGFloat = 100
    var images = [UIImage] ()


while i <4 {

if i = 1 {



var currentY: CGFloat + 10
    let width: CGFloat = 375
    let height: CGFloat = 100
    var images = [UIImage] ()
i + = 1

}

if i = 2 {

var currentY: CGFloat + 10
    let width: CGFloat = 375
    let height: CGFloat = 100
    var images = [UIImage] ()
}


这样吗?这不是我看到的代码,更像是一个伪代码,至少可以将我的想法收集到一些东西中)
要在屏幕上放置元素,我需要给它们在屏幕上的坐标,我应该将这些坐标放入变量中并通过一些步骤更改它们吗?就像每次进入循环时给所有坐标加 10 一样?
对于每个号码,我需要自己编写一段代码吗?比如 if = 1 then if 等于 2 then ...


谢谢!

【问题讨论】:

  • 这应该是带有自定义UICollectionViewCellUICollectionView

标签: swift loops uiimageview uilabel


【解决方案1】:

这就是完整的答案:)

 func loop() {
    var y: CGFloat = 0.0
    let height: CGFloat = 200
    let width: CGFloat = 180
    var x:CGFloat = 0.0
    let colorsss:[UIColor] = [.purple,.blue,.brown,.cyan]
    var image = UIImageView()
    for i in 0...3 {
        image = UIImageView(frame: CGRect(x: x, y: y, width: width, height: height))
        image.backgroundColor = colorsss[i]
        view.subviews(image)
        var label = UILabel()
        let colors:[UIColor] = [.red,.yellow,.orange,.black]
        for i in 0...3 {
            if i == 3 {
                label = UILabel(frame: CGRect(x: width - width / 4, y: 180, width: width / 4 - CGFloat(i), height: 20))
                label.backgroundColor = colors[i]
                image.subviews(label)
            } else {
                label = UILabel(frame: CGRect(x: 0, y: 120 + (i * 20), width: Int(width / 4 - CGFloat(i)), height: 20))
                label.backgroundColor = colors[i]
                image.subviews(label)
            }
            
        }
        if i == 0 {
            x = x + width
        } else if i == 1 {
            y = y + height
            x = 0
        } else  {
            x = x + width
            y = height
        }
    }
}

【讨论】:

  • 哇!谢谢!我在我的 VC 中尝试过,我在 image.subviews(label) 和 view.subviews(image) 行中得到了这个错误“无法调用非函数类型'[UIView]'的值”。为什么会出现这个问题?我做错事情了?谢谢!
  • 抱歉,我使用了 pod "Stevia" 你应该用 view.addSubviews(image) 和 view.addSubviews(label) 替换 view.subviews :)
  • 不幸的是,我仍然得到:“'UIView' 类型的值没有成员'addSubviews'”(
  • view.addSubview(image) 抱歉没有子视图它的子视图
  • 错误信息消失了!抱歉有这么多额外的问题,但是当我在模拟器中运行时,屏幕是空白的。为什么会这样?
【解决方案2】:

你可以通过两个 for 循环来做到这一点。第一个 FOR 循环用于每一行,第二个 FOR 循环用于设置每一行的图像视图。

var y: CGFloat = 0.0
var height: CGFloat = 200.0
var width: CGFloat = 150.0

for _ in 0...2 {
    var x: CGFloat = 0.0
    for _ in 0...2 {
       let imgView = UIImageView()
       imgView.frame = CGRect(x: x, y: y, width: width, height: height)
       self.view.addSubView(imgView)
       x = x + width
    }
    y = y + height
}

【讨论】:

  • 谢谢!我可以在图像之间添加标签吗?所以它们不会与它们重叠?
  • 您可以添加标签,但可以完美地声明标签框架。如果我的回答有效,请点赞。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多