九宫格---可滑动,可调整每行有几列,共有几行,边距可自适应
效果图如下所示:
#import "ViewController.h"
#define spaceLR (375-numRow*70)/(numRow+1) //左右两边的边距
#define spaceMD (375-numRow*70)/(numRow+1) //中间空白距
@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView * scroll;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark - 静态视图(不可滑动)
int numRow=3; //此时显示为3列,可以自己调整列数
int numCol=50; //此时显示为50行,可以自己调整行数,不能全部显示,可滑动查看
//添加scroll
_scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scroll.contentSize = CGSizeMake(375,numCol*120);
_scroll.pagingEnabled = YES;
_scroll.showsHorizontalScrollIndicator = NO;
_scroll.showsVerticalScrollIndicator = NO;
_scroll.bounces = NO;
_scroll.delegate = self;
for (int j=0; j<numCol; j++)
{
for (int i=0; i<numRow; i++)
{
UIView * view=[[UIView alloc]initWithFrame:CGRectMake(spaceLR+i*70+i*spaceMD, 20+j*120, 70, 120)];
UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d",arc4random_uniform(20)+256]];
[view addSubview: imageView];
UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(0, 75, 70, 20)];
label.text=@"下载";
label.textAlignment=NSTextAlignmentCenter;
label.font=[UIFont fontWithName:nil size:14];
[view addSubview:label];
[_scroll addSubview:view];
}
}
[self.view addSubview:_scroll];
}
@end
九宫格---可滑动,可调整每行有几列,共有几行,边距可自适应
原文地址:http://blog.csdn.net/qq_27364431/article/details/46046623