动态实现app的九宫格。
首先把素材拷进来,图片放在Assets.xcassets里,plist文件放进Supporting Files里面。不多说上代码。
// ViewController.h
// appList
//
// Created by 袁跃 on 16/4/28.
// Copyright © 2016年 iflytek. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
// appList
//
// Created by 袁跃 on 16/4/28.
// Copyright © 2016年 iflytek. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, strong) NSArray *apps;
@end
@implementation ViewController
//重写get方法,进行懒加载数据
- (NSArray *)apps{
if (_apps ==nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
_apps = [NSArray arrayWithContentsOfFile:path];
}
return _apps;
}
- (void)viewDidLoad {
[super viewDidLoad];
int cloumns = 3;
CGFloat viewWidth = self.view.frame.size.width;
CGFloat appW = 75;
CGFloat appH = 90;
CGFloat marginTop = 30;
CGFloat marginX = (viewWidth-cloumns*appW)/(cloumns+1);
CGFloat marginY = marginX;
for (int i=0; i<self.apps.count; i++) {
NSDictionary *appDict = self.apps[i];
//创建UIView
UIView *appView = [[UIView alloc]init];
//设置appview的属性
//appView.backgroundColor = [UIColor redColor];
CGFloat appX = marginX+((i%cloumns)*(appW+marginX));
CGFloat appY = marginTop+ ((i/cloumns)*(appH+marginY));
appView.frame = CGRectMake(appX, appY, appW, appH);
[self.view addSubview:appView];
UIImageView *appIcon = [[UIImageView alloc]init];
CGFloat appIconW = 45;
CGFloat appIconH = 45;
CGFloat appIconX = (appView.frame.size.width-appIconW)/2;
CGFloat appIconY = 0;
appIcon.frame = CGRectMake(appIconX, appIconY, appIconW, appIconH);
[appView addSubview:appIcon];
appIcon.image = [UIImage imageNamed:appDict[@"icon"]];
UILabel *labName = [[UILabel alloc]init];
CGFloat appLabW = appView.frame.size.width;
CGFloat appLabH = 20;
CGFloat appLabX = 0;
CGFloat appLabY = appIconH;
labName.frame = CGRectMake(appLabX, appLabY, appLabW, appLabH);
labName.text = appDict[@"name"];
labName.font = [UIFont systemFontOfSize:12];
labName.textAlignment = NSTextAlignmentCenter;
[appView addSubview:labName];
UIButton *appButton = [[UIButton alloc]init];
CGFloat appButtonX = appIconX;
CGFloat appButtonY = CGRectGetMaxY(labName.frame);
CGFloat appButtonW = appIconW;
CGFloat appButtonH = 20;
appButton.frame = CGRectMake(appButtonX, appButtonY, appButtonW, appButtonH);
[appButton setTitle:@"下载" forState:UIControlStateNormal];
[appButton setTitle:@"已下载" forState:UIControlStateDisabled];
[appButton setBackgroundImage:[UIImage imageNamed:@"buttongreen"]forState:UIControlStateNormal];
[appButton setBackgroundImage:[UIImage imageNamed:@"buttongreen_highted"]forState:UIControlStateHighlighted];
appButton.titleLabel.font = [UIFont systemFontOfSize:12];
[appButton addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[appView addSubview:appButton];
}
self.view.backgroundColor = [UIColor whiteColor];
}
-(void)buttonClick{
NSLog(@"正在下载");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
网友评论