create 10 red views in swift

To create 10 red views in Swift, you can use a UIStackView and add 10 UIView subviews to it. Here is an example code snippet:

main.swift
// Create a stack view
let stackView = UIStackView()

// Set stack view properties
stackView.axis = .vertical
stackView.spacing = 10
stackView.alignment = .center

// Loop to create 10 red views
for _ in 1...10 {
    // Create a view
    let view = UIView()

    // Set view properties
    view.backgroundColor = .red
    view.heightAnchor.constraint(equalToConstant: 50).isActive = true
    view.widthAnchor.constraint(equalToConstant: 50).isActive = true

    // Add view to stack view
    stackView.addArrangedSubview(view)
}

// Add stack view to a parent view
view.addSubview(stackView)

// Set stack view constraints
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
843 chars
30 lines

In this code, we create a UIStackView and set its properties. Then we loop through 10 times to create 10 red UIView subviews, set their properties, and add them to the stack view with the addArrangedSubview method. Finally, we add the stack view to a parent UIView and set its constraints. This will display the 10 red views in a vertical stack in the center of the parent view.

gistlibby LogSnag