The creation of nine Gongge is a regular one, and the horizontal and vertical intervals of each lattice are fixed, by calculating the position of each lattice, and then placing them in the view in turn.
The general steps are:
1. Set the total number of squares and the number of squares in each column
2. Interval of the lattice:
Horizontal interval = (total width of view-width of each grid * number of columns)/(Number of columns +1)
Vertical interval = Horizontal interval
3. With a For loop, create all the squares in turn and set the corresponding picture, and set the position of each grid.
The specific examples are as follows:
Create project projects with the name nine grid--swift
Import nine picture footage, file:
The code in Viewcontroller.swift operation is as follows:
1. Declaring properties
2. Set the total number of squares and the number of columns
3. Set the height and width of the grid
4. Set the spacing of the lattice
5. Create a nine grid
The demo results are as follows:
Swift: Create a nine lattice