九宫格---可滑动,可调整每行有几列,共有几行,边距可自适应

来源:互联网 发布:澳洲机场退税软件 编辑:程序博客网 时间:2024/05/16 06:28

九宫格---可滑动,可调整每行有几列,共有几行,边距可自适应


效果图如下所示:

     

#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 {

    [superviewDidLoad];

  

#pragma mark - 静态视图(不可滑动)

   int numRow=3;   //此时显示为3列,可以自己调整列数

   int numCol=50;  //此时显示为50行,可以自己调整行数,不能全部显示,可滑动查看

    

    //添加scroll

    _scroll = [[UIScrollViewalloc] 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=[[UIViewalloc]initWithFrame:CGRectMake(spaceLR+i*70+i*spaceMD,20+j*120, 70, 120)];

           UIImageView * imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(0,0, 70, 70)];

            imageView.image=[UIImageimageNamed:[NSStringstringWithFormat:@"%d",arc4random_uniform(20)+256]];

            [viewaddSubview: imageView];

           UILabel * label=[[UILabelalloc]initWithFrame:CGRectMake(0,75, 70, 20)];

            label.text=@"下载";

            label.textAlignment=NSTextAlignmentCenter;

            label.font=[UIFontfontWithName:nilsize:14];

            [viewaddSubview:label];

            [_scrolladdSubview:view];

        }

    }

    [self.viewaddSubview:_scroll];

}


@end

0 0
原创粉丝点击