开发者

Adding text above background image to uiswitch

开发者 https://www.devze.com 2023-04-13 04:08 出处:网络
My custom UISegmented control has background images [for each s开发者_Python百科tate]. How do I add text above the images [for each state, meaning that when index 0 will be selected I will have a spec

My custom UISegmented control has background images [for each s开发者_Python百科tate]. How do I add text above the images [for each state, meaning that when index 0 will be selected I will have a specific color and font] ?

Here's how my UISegmented control looks like:

UISegmentedControl *customUISC=[[UISegmentedControl alloc] initWithItems:[[[NSMutableArray alloc] initWithObjects:@"ON",@"OFF",nil] autorelease]];

    [customUISC setFrame:CGRectMake(11,14,298,28)];
    customUISC.selectedSegmentIndex=0;
    customUISC.segmentedControlStyle=UISegmentedControlStyleBar;

    [customUISC setImage:[UIImage imageNamed:@"FirstTabActive.png"] forSegmentAtIndex:0];
    [customUISC setImage:[UIImage imageNamed:@"SecondTab.png"] forSegmentAtIndex:1];
    [customUISC addTarget:self action:@selector(checkOnOffState:) forControlEvents:UIControlEventValueChanged];

and the checkOnOffState method looks like:

-(IBAction)checkOnOffState:(id)sender{

UISegmentedControl *iSeg=(UISegmentedControl *)sender;

if(iSeg.selectedSegmentIndex==0){

    [iSeg setImage:[UIImage imageNamed:@"FirstTabActive.png"] forSegmentAtIndex:0];
    [iSeg setImage:[UIImage imageNamed:@"SecondTab.png"] forSegmentAtIndex:1];
}
else {

    [iSeg setImage:[UIImage imageNamed:@"FirstTab.png"] forSegmentAtIndex:0];
    [iSeg setImage:[UIImage imageNamed:@"SecondTabActive.png"] forSegmentAtIndex:1];
}   
}


I'm not sure I fully understand what you're after, but could you just make use of UILabels (positioned above the images). You could then change the colours and text quite easily using myLabel.text and myLabel.textColor.

Hope this helps in some way!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号