build a 9 by 9 grid in swift

Here's a sample code to build a 9 by 9 grid using UIViews in Swift:

main.swift
let gridSize: CGFloat = 9
let gridHeight = containerView.bounds.height / gridSize
let gridWidth = containerView.bounds.width / gridSize

for i in 0..<Int(gridSize) {
    for j in 0..<Int(gridSize) {
        let gridCell = UIView(frame: CGRect(x: CGFloat(j) * gridWidth, y: CGFloat(i) * gridHeight, width: gridWidth, height: gridHeight))
        gridCell.layer.borderWidth = 0.5
        gridCell.layer.borderColor = UIColor.black.cgColor
        containerView.addSubview(gridCell)
    }
}
488 chars
13 lines

This code creates a 9 by 9 grid with the help of two nested for loops. We first calculate the height and width of a single cell by dividing the container view bounds with the grid size. Then, for each cell, we create a UIView with the calculated size and add it as a subview to the container view. We also set a black border for each cell for better visibility.

gistlibby LogSnag