0

我正在使用自定义背景图像自定义 UISegmentedControl。代码是这样的:

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

[[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];

但结果并非应有的样子。我不太确定该方法的用法:resizableImageWithCapInsets。附上图片。

它应该是这样的: 应该

但实际上是这样的: 截屏

我想我使用了错误的 UIEdgeInset 值。背景图像的宽度:22;高度:30,分隔图像的宽度:11;身高:30 有什么建议吗?

4

2 回答 2

2

尝试这个

HMSS段控制

仅使用这两个文件及其选中和取消选中的图像

HMSegmentedControl.h

HMSegmentedControl.m

.h 文件

    #import "HMSegmentedControl.h"
    HMSegmentedControl *segmentedControl;

.m 文件

viewDidLoad 的代码

    segmentedControl = [[HMSegmentedControl alloc] initWithSectionImages:@[[UIImage imageNamed:@"1"], [UIImage imageNamed:@"2"], [UIImage imageNamed:@"3"]] sectionSelectedImages:@[[UIImage imageNamed:@"1-selected"], [UIImage imageNamed:@"2-selected"], [UIImage imageNamed:@"3-selected"]]]; // it create size as per your segment total image
        [segmentedControl setSelectionIndicatorHeight:4.0f];
        [segmentedControl setFrame:CGRectMake(0, 45, 320, 45)];
        [segmentedControl setSegmentEdgeInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
        [segmentedControl setBackgroundColor:[UIColor blueColor]];
        [segmentedControl setSelectionLocation:HMSegmentedControlSelectionLocationDown];
        [segmentedControl setSelectionStyle:HMSegmentedControlSelectionStyleTextWidthStrip];
        [self.view addSubview:segmentedControl];



   - (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl1 {
       if (segmentedControl1.selectedSegmentIndex==0) {
          }
       else if (segmentedControl1.selectedSegmentIndex==1) {
           }
       else if(segmentedControl1.selectedSegmentIndex==2) {
             }
      }
于 2013-04-27T07:18:46.707 回答
0

我认为您应该使用更高质量的图像。

我来宾我使用了错误的 UIEdgeInset 值。背景图像的宽度:22;高度:30,分隔图像的宽度:11;身高:30 有什么建议吗?

您应该为图像尝试 60 * 60 像素。

于 2013-10-11T03:27:17.363 回答