UISegment控制背景图像用户更改

时间:2012-05-07 03:41:18

标签: iphone ios xcode

我希望我的应用程序有一个设置可以在用户按下UISegmented控件中的段时更改图像,但是,我只知道在Nav Bar中显示它。我将如何以一种特定的设置视图的方式实现它,当用户按下一个片段然后按下完成后,它将恢复为具有该背景图像的新视图。

3 个答案:

答案 0 :(得分:1)

试试这行代码。

 NSArray *ary=[sgmntControl subviews];
 NSInteger intCount=0;
 for (id seg in ary) 
  for (id imageview in [seg subviews]) 
 if ([imageview isKindOfClass:[UIImageView class]])
  {
   if(intCount==1)
    {
    [imageview setimage:[UIImage imagenamed:@""]; 

    }
    else 
    {
   [imageview setimage:[UIImage imagenamed:@""]; 

    }


   }

答案 1 :(得分:0)

您可以像这样更改分段控件的图像     UISegmentedControl * segment;

[segment setImage:UIImage  forSegmentAtIndex:segmentIndex];

[segment setBackgroundImage:UIImage  forState:ControlState barMetrics:UIBarMetrics];

修改

您可以尝试将此值传递给newViewController

- (IBAction)didChangeSegmentControl:(UISegmentedControl *)control {
   newViewController = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
newViewController. selectedIndex =control.selectedSegmentIndex;
[self.navigationController pushViewController: newViewController animated:NO];    
}
之前

你必须在newViewController中设置一个属性

@interface NewViewController : UIViewController
{
NSInteger selectedIndex;
}

@property(nonatomic)NSInteger selectedIndex;

@end

并在实施中

@implementation NewViewController

@synthesize selectedIndex;
//your codes
@end

答案 2 :(得分:0)

 One of the solution is you can add imageView under UISegmented controller with same size , set alpha for UISegmented 0.01 and change the imageview image in UISegmented action method.like below

self.imageViewSegment.image = [UIImage imageNamed:[NSString stringWithFormat:@"segment%d.png",self.segmentedControl.selectedSegmentIndex]];
//here your image names like segment0.png,segment1.png,segment2.png..etc