iOS 九宫格的实现

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(strong,nonatomic) UILabel *lblNums;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

 int x=50,y=50,width=50,height=50;

    int a=1;

    for (int i=0; i<3; i++)

    {

        for (int j=0;j<3; j++)

        {

            self.lblNums=[[UILabel alloc] initWithFrame:CGRectMake(x+j*55, y+i*55, width, height)];

            

           self.lblNums.backgroundColor=[UIColor colorWithRed:(arc4random()%12/11.0) green:(arc4random()%23/22.0) blue:(arc4random()%34/33.0) alpha:1.0];

            

            NSString *newnum=[@(a) stringValue];

                self.lblNums.text=newnum;

                self.lblNums.textAlignment=1;

                a++;

                [self.view addSubview:self.lblNums];

        }

    }

    

}

原文地址:https://www.cnblogs.com/tmf-4838/p/5330550.html