美文网首页很常
iOS 循环创建按钮(单行和多行)

iOS 循环创建按钮(单行和多行)

作者: 路边的风景呢 | 来源:发表于2019-09-27 11:20 被阅读0次

    创建单行的按钮

    CGFloatStart_X    =    15.0f;    // 第一个按钮的X坐标

        CGFloatStart_Y        =  _password.frame.origin.y+_password.frame.size.height+35;    // 第一个按钮的Y坐标

        CGFloatWidth_Space    =  15.0f;    // 2个按钮之间的横间距

        CGFloatButton_Height  =35.0f  ;  // 高

        CGFloatButton_Width  = (loginView.frame.size.width-45)/2;  // 宽

        NSArray* titleArray =@[@"取消",@"登录"];

        for(inti =0; i <2; i++) {

            // 圆角按钮

            UIButton* Button = [[UIButtonalloc]init];

            Button.tag= i;//这句话不写等于废了

            Button.frame=CGRectMake(i * (Button_Width + Width_Space) + Start_X,  Start_Y, Button_Width, Button_Height);

            Button.layer.masksToBounds=YES;

            Button.layer.cornerRadius=5;

            [ButtonsetTitle:titleArray[i]forState:UIControlStateNormal];

            if(i==0) {

                Button.backgroundColor= [UIColorcolorWithRed:148.0/255.0green:148.0/255.0blue:148.0/255.0alpha:1];

            }else{

             Button.backgroundColor= [UIColorcolorWithRed:79.0/255.0green:139.0/255.0blue:244.0/255.0alpha:1];

            }

            [ButtonaddTarget:selfaction:@selector(ButtonAction:)forControlEvents:UIControlEventTouchUpInside];

            [loginViewaddSubview:Button];

        }

    多行多列的按钮

    CGFloatStart_X  =15.0f;    // 第一个按钮的X坐标

        CGFloatStart_Y = whightView.Y+30;    // 第一个按钮的Y坐标

        CGFloatWidth_Space =15.0f;      // 2个按钮之间的横间距

        CGFloatHeight_Space=20.0f;    // 竖间距

        CGFloatButton_Height =40.0f;    // 高

        CGFloatButton_Width  =(SCREEN_WIDTH-45)/2;    // 宽

        NSArray* titleArray =@[@"失败",@"执行中",@"成功",@"中断"];

        _ButtonArray = [[NSMutableArray alloc]init];

        for(inti =0; i < titleArray.count

             ; i++) {

            NSIntegerindex = i %2;

            NSIntegerpage = i /2;

            // 圆角按钮

            UIButton*PlanTypeBtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

            PlanTypeBtn.tag= i;//这句话不写等于废了

            PlanTypeBtn.frame=CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);

            [PlanTypeBtnsetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

            [PlanTypeBtnsetTitle:titleArray[i]forState:UIControlStateNormal];

            PlanTypeBtn.layer.cornerRadius=5;

            PlanTypeBtn.layer.masksToBounds=YES;

            PlanTypeBtn.layer.borderWidth=1;

            PlanTypeBtn.layer.borderColor = RGBCOLOR(148,148,148).CGColor;

            [selfaddSubview:PlanTypeBtn];

            [_ButtonArray addObject:PlanTypeBtn];

            //按钮点击方法

            [PlanTypeBtnaddTarget:selfaction:@selector(PlanTypeBtnClick:)forControlEvents:UIControlEventTouchUpInside];

        }

        // 多个按钮的展示

        -(void)PlanTypeBtnClick:(UIButton*)btn{

            for(UIButton *btn1in_ButtonArray){

                if(btn1.tag == btn.tag){

                    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

                    btn1.layer.borderColor = RGBCOLOR(79,139,244).CGColor;

                    btn1.backgroundColor =RGBCOLOR(79,139,244);

                }else{

                    btn1.layer.borderWidth =1;

                    btn1.layer.borderColor = RGBCOLOR(148,148,148).CGColor;

                    btn1.backgroundColor =[UIColor clearColor];

                }

            }

        }

    相关文章

      网友评论

        本文标题:iOS 循环创建按钮(单行和多行)

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