button实现类似uisegment效果

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

    {

        //循环添加button

        UIButton * button = [custombutton initbutton:CGRectMake(i*80, 0, 80, 30) title:[[array objectAtIndex:i] valueForKey:@"typeName"]font:12 image:@"buttonCommon.png" select:@selector(touch:) who:self];

        //button添加tag

        button.tag=i+1;

        if (button.tag==1)

        {

            [button setBackgroundImage:[UIImage imageNamed:@"buttonSelect.png"] forState:UIControlStateNormal];

        }

        [scro addSubview:button];

    }



                         


 

-(void)touch:(id)sender

 

{  UIButton * button = (UIButton*)sender;

           //点击button更改背景

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

            {   //通过tag值取到button

                UIButton * butt =(UIButton *)[scro viewWithTag:i+1];

                [butt setBackgroundImage:[UIImage imageNamed:@"buttonCommon.png"] forState:UIControlStateNormal];

            }

           [button setBackgroundImage:[UIImage imageNamed:@"buttonSelect.png"] forState:UIControlStateNormal];    

}

 

你可能感兴趣的:(button,效果)