美文网首页iOS常用
ios创建多宫格界面方法(1)

ios创建多宫格界面方法(1)

作者: 浅_若清风 | 来源:发表于2021-07-29 18:08 被阅读0次

    iOS中,多宫格的界面很常见,实现的方法也有很多种,例如除了常见的UICollectionView方式,也还可以通过自定义一个UIView,在UIView中实现多宫格的创建,以下为我的工程中封装的多宫格创建代码,在此记录一下。

    完整代码

    1.自定义一个继承UIView的类JiugonggeView

    //JiugonggeView.h文件中实现
    @interface JiugonggeView : UIView
    //将点击按钮的事件传回给上一层
    @property (nonatomic, copy)void(^callBackTitle)(NSString *title);
    //视图的初始化
    - (instancetype)initWithFrame:(CGRect)frame andBtnTitleArray:(NSArray *)array;
    @end
    
    //JiugonggeView.m文件中实现
    #define IMAGENAMED(imgName) [UIImage imageNamed:imgName]
    @implementation JiugonggeView
    {
        CGFloat rowFloat;
    }
    //视图初始化
    - (instancetype)initWithFrame:(CGRect)frame andBtnTitleArray:(NSArray *)array
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            //创建按钮的边框
            rowFloat = 4.00;//这里表示行数为4
            for (NSInteger i = 0; i < rowFloat; i ++)
            {
                //每行列数为3
                for (NSInteger j = 0; j < 3; j ++)
                {
                    CGFloat btnW =  frame.size.width/3.00;
                    CGFloat btnH = frame.size.height/rowFloat-0.3;
                    CGFloat  btnX = j*btnW;
                    CGFloat btnY = I*btnH;
                    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
                    [backView.layer setBorderColor:RGB_HEX(0xe3e3e3, 1).CGColor];
                    [backView.layer setBorderWidth:0.3];
                    [self addSubview:backView];
                }
            }
            [self createBtn:array];
        }
        return self;
    }
    //创建按钮
    - (void)createBtn:(NSArray *)array
    {
        //计算行数
        NSInteger myCount = [array count]/3;
        if ([array count]%3 > 0)
        {
            myCount += 1;
        }
        //从数组中取标题的索引
        NSInteger index = 0;
        //行循环
        for (NSInteger i = 0; i <myCount; i ++)
        {
            //计算列数
            NSInteger num = 3;
            if (i == myCount-1 && [array count]%3 > 0)
            {
                num = [array count]%3;
            }
            //列循环
            for (NSInteger j = 0; j < num; j ++)
            {
                //设置按钮的位置参数
                CGFloat btnW =  self.frame.size.width/3.00;
                CGFloat btnH = self.frame.size.height/rowFloat;
                CGFloat  btnX = j*btnW;
                CGFloat btnY = I*btnH;
                //创建按钮
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setFrame:CGRectMake(btnX, btnY, btnW, btnH)];
                [btn setBackgroundColor:[UIColor clearColor]];
                [btn addTarget:self action:@selector(btnFunction:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:btn];
                //设置按钮中图片文字
                NSArray *ary = array[index];
                [btn setTitle:ary[1] forState:UIControlStateNormal];
                [btn.titleLabel setFont:[UIFont systemFontOfSize:14 weight:UIFontWeightLight]];
                [btn.titleLabel setNumberOfLines:0];
                [btn.titleLabel setTextAlignment:NSTextAlignmentCenter];
                [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [btn setImage:IMAGENAMED(ary[0]) forState:UIControlStateNormal];
                //设置按钮中图片在上文字在下
                CGFloat offset = 14.0f;
                btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width, -btn.imageView.frame.size.height-offset/2, 0);
                btn.imageEdgeInsets = UIEdgeInsetsMake(-btn.titleLabel.intrinsicContentSize.height-offset/2, 0, 0, -btn.titleLabel.intrinsicContentSize.width);
                //设置从数组中取标题的索引
                index ++;
            }
        }
    }
    //点击按钮
    - (void)btnFunction:(UIButton *)sender
    {
        if (self.callBackTitle)
        {
            self.callBackTitle(sender.titleLabel.text);
        }
    }
    

    2.在VC中调用

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self createScrollView];
    }
    //创建视图
    - (void)createScrollView
    {
         NSArray *dataAry = @[@[@"res_ico_Report",@"按钮1"],@[@"oth_ico_app",@"按钮2"],@[@"oth_ico_dtc",@"按钮3"],@[@"res_ico_batterydataplayback",@"按钮4"],@[@"res_ico_dataplayback",@"按钮5"]];
        //创建功能按钮视图
        JiugonggeView *funcView = [[JiugonggeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) andBtnTitleArray: dataAry];
        [funcView setBackgroundColor:[UIColor clearColor]];
        [self.view addSubview:funcView];
        //点击按钮后的回调
        [funcView setCallBackTitle:^(NSString *title)
        {
            [self funcButtonClickAction:title];
        }];
    }
    //实现按钮方法
    - (void)funcButtonClickAction:(NSString *)title
    {
    }
    
    效果图
    img_1.png
    UICollectionView实现多宫格界面

    相关文章

      网友评论

        本文标题:ios创建多宫格界面方法(1)

        本文链接:https://www.haomeiwen.com/subject/qykfvltx.html