0

我有 UISegmentedControl 对象,我在其中获取了图像数组。现在我想在选择每个对象后更改这些图像,例如 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];
4

1 回答 1

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 自定义

于 2013-01-04T10:07:23.963 回答