您的位置:首页 > 产品设计 > UI/UE

在UIView中添加应用下载信息模块

2015-08-13 09:08 429 查看
-(void)willMoveToSuperview:(UIView *)newSuperview
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"books.plist" ofType:nil];
_books = [NSArray arrayWithContentsOfFile:path];

NSLog(@"%@",_books);

self.frame = newSuperview.bounds;
self.backgroundColor = [UIColor redColor];
int totalColumns = 3;

// 应用的尺寸
CGFloat bookW = 90;
CGFloat bookH = 125;

// 间隙 = (view的宽度 - 3 * 应用宽度) / 4
CGFloat marginX = (self.frame.size.width - totalColumns * bookW) / (totalColumns + 1);
CGFloat marginY = 15;

// 根据应用个数创建对应的框(index)
for (int index = 0; index<self.books.count; index++) {
// 创建
UIView *bookView = [[UIView alloc] init];
// 设置背景色
bookView.backgroundColor = [UIColor cyanColor];

// 计算框的位置
// 计算行号和列号
int row = index / totalColumns;
int col = index % totalColumns;
// 计算x和y
CGFloat bookX = marginX + col * (bookW + marginX);
CGFloat bookY = 20 + row * (bookH + marginY);
// 设置frame
bookView.frame = CGRectMake(bookX, bookY, bookW, bookH);

//添加框到控制器的view
[self addSubview:bookView];

// 添加控件
// index位置对应的应用信息
NSDictionary *bookInfo = self.books[index];

// 添加图片
//UIImageView *iconView = [[UIImageView alloc] init];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:bookInfo[@"icon"]] forState:UIControlStateNormal];
// 设置位置
CGFloat iconW = 60;
CGFloat iconH = 80;
CGFloat iconX = (bookW - iconW) * 0.5;
CGFloat iconY = 0;
btn.frame = CGRectMake(iconX, iconY, iconW, iconH);
[bookView addSubview:btn];

// 添加名字
UILabel *nameLabel = [[UILabel alloc] init];
// 设置位置
CGFloat nameW = bookW;
CGFloat nameH = 20;
CGFloat nameX = 0;
CGFloat nameY = iconY + iconH;
nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH);
// 设置文字
nameLabel.text = bookInfo[@"name"];
// 设置字体
nameLabel.font = [UIFont systemFontOfSize:13];

// 设置文字居中对齐
nameLabel.textAlignment = NSTextAlignmentCenter;
[bookView addSubview:nameLabel];

// 添加下载按钮
UIButton *downloadBtn = [[UIButton alloc] init];
// 设置位置
CGFloat downloadX = 12;
CGFloat downloadY = nameY + nameH;
CGFloat downloadW = bookW - 2 * downloadX;
CGFloat downloadH = 20;
downloadBtn.frame = CGRectMake(downloadX, downloadY, downloadW, downloadH);
// 设置默认的背景

UIImage *normalImage = [UIImage imageNamed:@"upomp_button_keyboard3"];
[downloadBtn setBackgroundImage:normalImage forState:UIControlStateNormal];
// 设置高亮的背景
UIImage *highImage = [UIImage imageNamed:@"upomp_button_keyboard3_highlighted"];
[downloadBtn setBackgroundImage:highImage forState:UIControlStateHighlighted];
// 设置按钮的文字
[downloadBtn setTitle:@"download" forState:UIControlStateNormal];

// 设置按钮文字的字体

downloadBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[bookView addSubview:downloadBtn];
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: