我想在选择后更改UISegmentedControl图像

时间:2013-01-04 09:48:33

标签: ios image nsarray uisegmentedcontrol

我有UISegmentedControl对象,其中我使用了images数组。现在我想在选择每个对象(如UIButton控件)之后更改此图像,我们可以在其中设置图像以供选择以及非选择。 这是我的代码

NSArray *itemArray;
        itemArray = [NSArray arrayWithObjects:
                     [UIImage imageNamed:@"grey.png"],
                     [UIImage imageNamed:@"overlay.png"],
                     [UIImage imageNamed:@"marker.png"],
                     nil];

    segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [segmentedControl addTarget:self action:@selector(segmentedControlIndexChanged) forControlEvents:UIControlEventValueChanged];

1 个答案:

答案 0 :(得分:0)

您可以使用自己的图像自定义分段控制,以便在iOS 5中选择状态和正常状态 试试这个:

 UIImage *segmentSelected = 
[[UIImage imageNamed:@"segcontrol_sel.png"] 
    resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
 UIImage *segmentUnselected = 
[[UIImage imageNamed:@"segcontrol_uns.png"] 
    resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
 UIImage *segmentSelectedUnselected = 
[UIImage imageNamed:@"segcontrol_sel-uns.png"];
  UIImage *segUnselectedSelected = 
[UIImage imageNamed:@"segcontrol_uns-sel.png"];
UIImage *segmentUnselectedUnselected = 
[UIImage imageNamed:@"segcontrol_uns-uns.png"];

[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected 
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  [[UISegmentedControl appearance] setBackgroundImage:segmentSelected 
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

     [[UISegmentedControl appearance] setDividerImage:segmentUnselectedUnselected 
forLeftSegmentState:UIControlStateNormal 
rightSegmentState:UIControlStateNormal 
barMetrics:UIBarMetricsDefault];
 [[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected 
forLeftSegmentState:UIControlStateSelected 
rightSegmentState:UIControlStateNormal 
barMetrics:UIBarMetricsDefault];
  [[UISegmentedControl appearance] 
setDividerImage:segUnselectedSelected 
forLeftSegmentState:UIControlStateNormal 
rightSegmentState:UIControlStateSelected 
barMetrics:UIBarMetricsDefault];

您还可以在http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

检查完整的UI自定义