zoukankan      html  css  js  c++  java
  • iOS 自定义九宫格

    /**
         在此我们使用的是Button来做
         因为很多时候需要文字也需要图片显示,所以Button比较合适
         */
    
    - (void)CreatorBtn
    {
        //列数
        NSInteger column = 4;
        //按钮个数
        NSInteger buttonCount = 18;
        
        //按钮的宽高
        CGFloat buttonW = self.view.frame.size.width / column;
        CGFloat buttonH = buttonW;
        
        for (int i = 0; i < buttonCount; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            
            btn.frame = CGRectMake(((i % column) * buttonW), ((i / column) * buttonH), buttonW - 10, buttonH - 10);
            
            
            NSLog(@"%@",NSStringFromCGRect(btn.frame));
            
            [btn setBackgroundColor:[UIColor redColor]];
            
            //绑定tag,后边监听点击
            btn.tag = i;
            
            [self.view addSubview:btn];
            
            
            //监听按钮点击
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            
        }
        
        
    }
    
    - (void)btnAction:(UIButton *)btn
    {
        //根据按钮的tag来监听点击
        NSLog(@"点击了第%ld个按钮",(long)btn.tag);
        
    }
    

     

  • 相关阅读:
    Redux 学习总结
    ECMAScript 6 学习总结
    Bootstrap 前端UI框架
    React.js 学习总结
    html 之 <meta> 标签之http-equiv
    Leetcode Excel Sheet Column Number (C++) && Excel Sheet Column Title ( Python)
    490
    414
    494
    458
  • 原文地址:https://www.cnblogs.com/hkyangvip/p/5136487.html
Copyright © 2011-2022 走看看