iOS:多个按钮切换状态、改变颜色字体等

2018-02-24  本文已影响32人  iOS技术小卒

循环多个按钮

for (int i = 0; i < headertitlearray.count; i + +) {

uibutton * headbtn = [[uibutton alloc] initwithframe: cgrectmake (* costomerthiswidth, 0, costomerthiswidth, 44)];

[headbtn settitle: [nsstring stringwithformat: @ "% @", headertitlearray []] forstate: uicontrolstatenormal];

[headbtn addtarget: self action: @ selector (clicktitlebtn:) forcontrolevents: uicontroleventtouchupinside];

/ / headbtn.backgroundcolor = [uicolor greencolor];

headbtn.titlelabel.font = [uifont systemfontofsize: 14].

[headbtn settitlecolor: [uicolor whitecolor] forstate: uicontrolstatenormal];

[headbtn settitlecolor: [uicolor orangecolor] forstate: uicontrolstateselected];

[headsscrollview addsubview: headbtn];

headbtn.tag = 10000 + i;

/ / headbtn.selected = no;

}

实现按钮点击事件

-(void)clickTitleBTN:(UIButton *)button1{

    NSLog(@"%d",(int)mtage);

  //=====第一种方式

/***  按钮选中,中间值*/

先去定义一个按钮属性@property (nonatomic,strong) UIButton *selectedBtn;

//    if (button1!= self.selectedBtn) {

//        //button1.titleLabel.font=[UIFont systemFontOfSize:17];

//        self.selectedBtn.selected = NO;

//        button1.selected = YES;

//        self.selectedBtn = button1;

//    }else{

//        //button1.titleLabel.font=[UIFont systemFontOfSize:14];

//        self.selectedBtn.selected = YES;

//       

//    }

  //=====第二种方式 (更好,字体、颜色都可以控制)

    for (int i = 0; i < headerTitleArray.count; i++) {

        UIButton *btn = (UIButton *)[[button1 superview]viewWithTag:10000 + i];

        [btn setSelected:NO];

        btn.backgroundColor=[UIColor clearColor];

        btn.titleLabel.font=[UIFont systemFontOfSize:14];

    }

    UIButton *button = (UIButton *)button1;

    [button setSelected:YES];

    button.backgroundColor=[UIColor blackColor];

    button.titleLabel.font=[UIFont systemFontOfSize:17];

}

上一篇 下一篇

猜你喜欢

热点阅读