首先自己创建一个polist文件 里面创建自己需要的数组和字典(这里我是根据自己的需求来创建的,如有什么不对的希望各位指点)
如图所示(里面的九宫格的图片和名称我都是存放到 自己新建的 home.polist 文件里面的 如何获取它们呢 我在这里简单的为大家写一下)
代码:
首先我们先解析获取polist文件里面的内容
//解析加载按钮数据home.plist
NSString *path=[[NSBundle mainBundle] pathForResource:@"home" ofType:@"plist"];
NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:path];
NSArray *arrayImages=[dic objectForKey:@"images_btn"]; //获取polist文件里面的图片
NSArray *arrayText=[dic objectForKey:@"labels_btn"]; //获取polist文件里面的名称
定义一个button 利用fou循环来赋值
for (int i=0; i<9; i++) {
NSInteger index_x=i%3;//x 012012012
NSInteger page_y=i/3;//y 000111222
UIbutton *btn=[[UIbutton alloc]init];
[btn setTag:i];
[btn setFrame:CGRectMake(35*WidthMatch+index_x*(btnWidth+horizontalSpacing),self.imageV.frame.size.height+25*HeightMatch+page_y*(btnHeight+verticalSpacing), btnWidth, btnHeight)];
[btn setImage:[UIImage imageNamed:arrayImages[i]] forState:UIControlStateNormal]; //给每一个button添加图片
//对设置按钮重新调整
switch (btn.tag) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
//标题的位置
[btn setLabelText1:arrayText[btn.tag] frame:CGRectMake(1*WidthMatch, btnHeight+10*HeightMatch, 50*WidthMatch, 10*HeightMatch)]; //利用tag值给button赋值
break;
case 6:
[btn setLabelText1:arrayText[btn.tag] frame:CGRectMake(1*WidthMatch, btnHeight+10*HeightMatch, 50*WidthMatch, 10*HeightMatch)];
break;
case 8:
[btn setLabelText1:arrayText[btn.tag] frame:CGRectMake(1*WidthMatch, btnHeight+10*HeightMatch, 50*WidthMatch, 10*HeightMatch)];
break;
case 7:
[btn setLabelText1:arrayText[7] frame:CGRectMake(1*WidthMatch, btnHeight+10*HeightMatch, 50*WidthMatch, 10*HeightMatch)];
break;
default:
break;
}
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}