iOS 九宫格显示图片按钮

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    //将每个图片作为一格,通过控制它的位置来实现九宫格。

    NSArray* imageNames = [NSArray arrayWithObjects:@”1.png”,@”2.png”,@”3.png”,@”4.png”,@”5.png”,@”1.png”,@”1.png”,@”1.png”,@”1.png”, nil];

    

    for (int i=0; i<9; i++) {

        UIButton *Btn = [[UIButton alloc] init];

        //[Btn setImage:[UIImage imageNamed:[imageNames objectAtIndex: i]] forState:UIControlStateNormal];//设置按钮图片

        [Btn setBackgroundImage:[UIImage imageNamed:[imageNames objectAtIndex: i]] forState:UIControlStateNormal];

        Btn.tag = i;

        

        CGRect frame;

        frame.size.width = 59;//设置按钮坐标及大小

        frame.size.height = 75;

        frame.origin.x = (i%3)*(59+32)+40;

        frame.origin.y = floor(i/3)*(75+24)+40;

        [Btn setFrame:frame];

        

        [Btn setBackgroundColor:[UIColor clearColor]];

        [Btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:Btn];

    }

}

//响应按钮事件

-(void)btnPressed:(id)sender{

    

}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注