首先自己創(chuàng)建一個(gè)polist文件 ? 里面創(chuàng)建自己需要的數(shù)組和字典(這里我是根據(jù)自己的需求來(lái)創(chuàng)建的,如有什么不對(duì)的希望各位指點(diǎn))
如圖所示(里面的九宮格的圖片和名稱我都是存放到 自己新建的 home.polist ?文件里面的 ?如何獲取它們呢 我在這里簡(jiǎn)單的為大家寫(xiě)一下)
代碼:
首先我們先解析獲取polist文件里面的內(nèi)容
//解析加載按鈕數(shù)據(jù)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文件里面的名稱
定義一個(gè)button ? 利用fou循環(huán)來(lái)賦值
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]; ? //給每一個(gè)button添加圖片
//對(duì)設(shè)置按鈕重新調(diào)整
switch (btn.tag) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
//標(biāo)題的位置
[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];
}