Make a simple nine Gongge:
The source code is as follows:
1 #import "ViewController.h" 2 3 @interface Viewcontroller () 4 5 @end 6 7 @implementation Viewcontroller 8 9-(void ) Viewdidload {Ten [Super Viewdidload];11 12//create nine Nsinteger col = 3; Number of rows per row Nsinteger total = 9;//15 16//width of grid, height UIImage *image = [UIImage imagenamed:@ "0.png"];18 CGFloat cellwidth = image.size.width;19 cgfloat cellheight = image.size.height;20 cgfloat screenWidth = SE lf.view.frame.size.width;22 cgfloat gapx = (screenwidth-cellwidth*col)/(col+1),//x direction interval cgfloat GapY = GA px;//y direction Interval 25 26 27//Count rows Nsinteger row = 0;29 for (int i=0; i<total; i++) 31 {32 UIButton *button = [[UIButton alloc]init];33 [button setbackgroundimage:[uiimage imagenamed:[nsstring stringwit hformat:@ "%d.png", I]] forstate:uicontrolstatenormal];34 35//calculates if a newline is required (I%col = = 0) 37 { row++;39}40 cgfloat x = gapx+(CELLWIDTH+GAPX) * (i%col); CGFloat y = gapy+ (cellheight+gapy) *row;42 button.frame = CGRectMake (x, Y, cell Width, cellheight); [Self.view addsubview:button];45}46}47 @end
IOS: Making nine grid