#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self creatPic];
}
- (void)creatPic
{
for (int i=0; i<3; i++) {
//创建button
UIButton *btn1=[[UIButton alloc] init];
btn1.frame=CGRectMake(50, 50+i*180, 300, 150);
[self.view addSubview:btn1];
btn1.backgroundColor=[UIColor cyanColor];
//创建UIImageView
UIImageView *iv1=[[UIImageView alloc] init];
iv1.frame=CGRectMake(20, 20, 180, 110);
[btn1 addSubview:iv1];
iv1.backgroundColor=[UIColor blueColor];
iv1.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
iv1.contentMode=UIViewContentModeScaleToFill;
iv1.alpha=0.8;
//创建按钮
UILabel *label1=[[UILabel alloc] init];
UILabel *label2=[[UILabel alloc] init];
label1.text=@"名字";
label2.text=@"详情";
label1.textAlignment=NSTextAlignmentCenter;
label2.textAlignment=NSTextAlignmentCenter;
label1.textColor=[UIColor blackColor];
label2.textColor=[UIColor blackColor];
label1.frame=CGRectMake(270, 120+i*180, 60, 25);
label2.frame=CGRectMake(270, 160+i*180, 60, 25);
label1.backgroundColor=[UIColor grayColor];
label2.backgroundColor=[UIColor greenColor];
[self.view addSubview:label1];
[self.view addSubview:label2];
}
}
//这是.jpg图片
这里在一个循环三次的函数中创建了button,imageView等
//这是最后的运行结果