以编程方式添加重叠的uibutton

时间:2014-10-15 12:50:00

标签: ios

我有一系列Plus按钮和另一个以编程方式添加的Minus按钮数组。减号按钮完全位于加号按钮下。代码是:

    int x=0;
    for (int t=0;t<=14;t++){
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonPressedPlus:)                forControlEvents:UIControlEventTouchUpInside];
     button.tag = t;
    [button setTitle:@"" forState:UIControlStateNormal];
     button.frame = CGRectMake(270, x+10, 13, 13);
    [button setBackgroundColor:[UIColor colorWithRed: 10.0/255.0f green:100.0/255.0f   blue:150.0/255.0f alpha:1.0f]];
     button.layer.cornerRadius = 10;
    [button setBackgroundImage:[UIImage imageNamed:@"Plus"] forState:UIControlStateNormal];
    [segmentedView1 addSubview:button];
    [self.view addSubview:segmentedView1];
    x+=20;

    }



    int x1=0;
    for (int t1=0;t1<=14;t1++){
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button addTarget:self action:@selector(buttonPressedMinus:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = t1;
        [button setTitle:@"" forState:UIControlStateNormal];
        button.frame = CGRectMake(270, x1+10, 13, 13);
        [button setBackgroundColor:[UIColor colorWithRed: 10.0/255.0f green:100.0/255.0f blue:150.0/255.0f alpha:1.0f]];
        button.layer.cornerRadius = 10;
        [button setBackgroundImage:[UIImage imageNamed:@"Minus"] forState:UIControlStateNormal];
        [segmentedView1 addSubview:button];
        [self.view addSubview:segmentedView1];
        x1+=20;
    }

现在我的任务是..当按下一个加号按钮时,它下方的减号按钮应该是可见的,一旦按下减号按钮,再按一次按钮应该是可见的。 怎么做?

1 个答案:

答案 0 :(得分:0)

按钮重叠,因为在这两种情况下你的x = 0; 您可以在单个for循环中执行此操作。

   int x=0;
        for (int t=0;t<=14;t++){
    //plus button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button addTarget:self action:@selector(buttonPressedPlus:)                forControlEvents:UIControlEventTouchUpInside];
         button.tag = t;
        [button setTitle:@"" forState:UIControlStateNormal];
         button.frame = CGRectMake(270, x+10, 13, 13);
        [button setBackgroundColor:[UIColor colorWithRed: 10.0/255.0f green:100.0/255.0f   blue:150.0/255.0f alpha:1.0f]];
         button.layer.cornerRadius = 10;
        [button setBackgroundImage:[UIImage imageNamed:@"Plus"] forState:UIControlStateNormal];
        [segmentedView1 addSubview:button];
        [self.view addSubview:segmentedView1];
        x+=20;

    // minus button 
     UIButton *buttonM = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [buttonM addTarget:self action:@selector(buttonPressedMinus:) forControlEvents:UIControlEventTouchUpInside];
        buttonM.tag = t1+1000;
        [buttonM setTitle:@"" forState:UIControlStateNormal];
        buttonM.frame = CGRectMake(270, x+10, 13, 13);
        [buttonM setBackgroundColor:[UIColor colorWithRed: 10.0/255.0f green:100.0/255.0f blue:150.0/255.0f alpha:1.0f]];
        buttonM.layer.cornerRadius = 10;
        [buttonM setBackgroundImage:[UIImage imageNamed:@"Minus"] forState:UIControlStateNormal];
        [segmentedView1 addSubview:buttonM];
        [self.view addSubview:segmentedView1];
        x+=20;
        }

//
-(void)buttonPressedPlus:(id)sender{
// in this show minus button please also take care of tag ex:-1000
// in this hide plus button please also take care of tag ex:-0.
}
-(void)buttonPressedMinus:(id)sender{
// in this hide minus button please also take care of tag
// in this show plus button please also take care of tag.
}