删除UISegmentedControl的框架

时间:2014-05-31 23:26:41

标签: ios objective-c ios7 uikit uisegmentedcontrol

iOS 7中是否有办法完全删除UISegmentedControl的外框?所选片段的色调颜色应该只有一个矩形。

1 个答案:

答案 0 :(得分:3)

首先,创建三个图像并将它们添加到您的图像资源中。

  • 空白(任何大小的透明png)
  • 突出显示(任何颜色和大小的png)
  • 选择(任何颜色和大小的png)

Xcode Image Assets Catalog showing Blank, Highlight, and Select image sets added

然后添加此代码:

UIImage *backgroundImage = [UIImage imageNamed:@"Blank"];
UIImage *highlightedImage = [UIImage imageNamed:@"Highlight"];
UIImage *selectedImage = [UIImage imageNamed:@"Select"];
UIImage *dividerImage = [[UIImage alloc] init];
[segmentedControl setBackgroundImage:backgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:highlightedImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:selectedImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[segmentedControl setDividerImage:dividerImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

UISegmentedControl with clear background and no frame. Selected color is blue. Highlighted color is light blue.