使用resizableImageWithCapInsets自定义UISegmentedControl

时间:2013-04-27 06:56:16

标签: ios uisegmentedcontrol

那里。 我使用自定义背景图像自定义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。 图像附加。

应该是这样的: should be

但实际上它是这样的: screen shot

我是guest,我使用了错误的UIEdgeInset值。 背景图片的宽度为:22;身高:30 并且分频器图像的宽度为:11;身高:30 有什么建议吗? 非常感谢提前。

2 个答案:

答案 0 :(得分:2)

试试这个

HMSSegmentControl

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

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) {
             }
      }

答案 1 :(得分:0)

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

  

我是guest,我使用了错误的UIEdgeInset值。背景图片的宽度为:22;高度:30,分隔线图像宽度:11;身高:30有什么建议吗?非常感谢。

你应该尝试60 * 60像素的图像。