效果如下,就是在一个空白页面动态添加控件,给按钮添加事件,图片名字和标题放入plist文件,plist是个Array,每一项是Dictionary。Dictionary里面方icon和name两个String的key。图片都放入Assets.xcassets。如果需要使用imageWithContentsOfFile方法(无缓存)加载图片,那么就需要新建Supporting Files个Group,将文件夹托进去,使用imageNamed方法(有缓存),那么只需将图片拖入Assets.xcassets文件夹即可。
给出代码:
//
// AddViewController.m
// study2024
//
// Created by zhifei zhu on 2024/8/31.
//
#import "AddViewController.h"
@interface AddViewController ()
@property (nonatomic,strong) NSArray *iconArray;
@end
@implementation AddViewController
- (NSArray *)iconArray{
if(_iconArray==nil){
NSString *path=[[NSBundle mainBundle]pathForResource:@"icons.plist" ofType:nil];
_iconArray=[NSArray arrayWithContentsOfFile:path];
}
return _iconArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSUInteger count=self.iconArray.count;
for(int a=0;a<count;a++){
//添加外边框
UIView *uiView=[UIView new];
uiView.backgroundColor=[UIColor blueColor];
CGFloat x=50+(a%3)*(75+10);
CGFloat y=50+(a/3)*(100+10);
uiView.frame=CGRectMake(x, y, 75, 100);//x,y,w,h
//外边框内部添加图片
UIImageView *uiImageView=[UIImageView new];
uiImageView.backgroundColor=[UIColor greenColor];
CGFloat iconW=45;
CGFloat iconH=45;
CGFloat x1=(uiView.frame.size.width-iconW)*0.5;//相对坐标
CGFloat y1=0;//相对坐标
uiImageView.frame=CGRectMake(x1, y1, iconW, iconH);//x,y,w,h
NSDictionary *dictionary=self.iconArray[a];
// NSString *imgPath = [NSString stringWithFormat:@"%@/%@.jpg", [[NSBundle mainBundle] resourcePath], dictionary[@"icon"]];
NSString *imgPath=[[NSBundle mainBundle]pathForResource:dictionary[@"icon"] ofType:@"jpg"];
//照片拖入Assets.xcassets文件夹会找不到资源,注意需要项目下新建group命名为Supporting Files,再项目外新建文件夹比如icons,然后将图片放入icons,再将icons文件夹拖入Supporting Files才能找到,否则返回nil
UIImage *uiImage=[UIImage imageWithContentsOfFile:imgPath];//imageWithContentsOfFile不会缓存,每次都重新加载图片
// UIImage *uiImage=[UIImage imageNamed:dictionary[@"icon"]];//imageNamed会缓存,照片拖入Assets.xcassets文件夹即可,图片非常多,会占用很多内存
uiImageView.image=uiImage;
[uiView addSubview:uiImageView];
//外边框内部标题
UILabel *uiLabel=[UILabel new];
uiLabel.backgroundColor=[UIColor yellowColor];
CGFloat labelW=uiView.frame.size.width;
CGFloat labelH=20;
CGFloat x2=0;//相对坐标
CGFloat y2=uiImageView.frame.size.height+5;//相对坐标
uiLabel.frame=CGRectMake(x2, y2, labelW, labelH);//x,y,w,h
uiLabel.text=dictionary[@"name"];
[uiLabel setTextAlignment:NSTextAlignmentCenter];
[uiView addSubview:uiLabel];
//外边框内部添加按钮
UIButton *uiButton=[UIButton new];
uiButton.backgroundColor=[UIColor redColor];
CGFloat buttonW=55;
CGFloat buttonH=20;
CGFloat x3=(75-55)*0.5;//相对坐标
CGFloat y3=uiImageView.frame.size.height+uiLabel.frame.size.height+5+5;//相对坐标
uiButton.frame=CGRectMake(x3, y3, buttonW, buttonH);//x,y,w,h
[uiButton setTitle:@"下载" forState:UIControlStateNormal];
[uiButton addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
uiButton.tag=a;
[uiView addSubview:uiButton];
[self.view addSubview:uiView];
}
}
-(void)onclick:(UIButton *)uiButton{
NSLog(@"%d点击下载",uiButton.tag);
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end