Home > Mobile >  Loop to create UIImageView and Labels in Swift
Loop to create UIImageView and Labels in Swift

Time:10-05

Hello!


I have a homework in which I need to using a loop, generate four UIImageViews and 16 labels on the screen as shown in the picture(no additional elements need to be generated). VC example of how it should look


So, generally, I think I understand how to do it, but I have some questions: I need to use a for i in 0 ... 3 loop in which I say that var image = UIImageView ()?


or

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] ()
}


Like that? It's not a code as I see it, more of a pseudo-code to gather my thoughts into at least something)
To place elements on the screen, I need to give them coordinates on the screen, should I put these coordinates into variables and change them with some step?Like each time you enter the loop add 10 to all coordinates?
For each number do I need to do my own piece of code? like if = 1 then if equal to 2 then ...


Thank you!

CodePudding user response:

You can do this by two for loop. First FOR loop is for each row and the second FOR loop is for setting each row image view.

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
}

CodePudding user response:

thats the full answer :)

 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
        }
    }
}
  • Related