简体   繁体   English

UISegmentedControl背景图片奇怪的行为

[英]UISegmentedControl background image strange behavior

I'm working on an iOS project and came across this strange issue. 我正在开发一个iOS项目,遇到了这个奇怪的问题。 I have a UISegmentedControl element of bar style. 我有一个bar风格的UISegmentedControl元素。 It has 3 segments. 它分为3个部分。 The segments have fixed sizes of 80. I also have 3 images with each segment selected to set as background. 这些段的固定大小为80。我还有3张图像,每个段都选择设置为背景。 Here is one of them` 这是其中之一。

在此处输入图片说明

When view is loaded, one of the segments is set as selected and this image is set as background like this: 加载视图时,将细分之一设置为选中状态,并将该图像设置为背景,如下所示:

[self.genderSelectionButton setBackgroundImage:[UIImage imageNamed:@"gender-switch01.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Now when I run the project on 5.1 Simulator, I get this distorted view for segmented button: 现在,当我在5.1 Simulator上运行项目时,我得到了分段按钮的变形视图:

在此处输入图片说明

As you can see, the middle segment keeps the size I gave, but the image is kind of stretched from the middle to the edges. 如您所见,中间部分保持了我给的大小,但是图像有点从中间延伸到边缘。 Also selected segment is highlighted even though I have set it to NO . 即使我将其设置为NO选中的段也会突出显示。 In the method that listens to button selection, I change background image and get the following: 在侦听按钮选择的方法中,我更改背景图像并获得以下内容: 在此处输入图片说明

(background image is stretched again...) (背景图像再次被拉伸...)

Almost the same happens on iOS6. 在iOS6上几乎相同。 Now am I doing something wrong, or is there a way to fix this?? 现在我做错了什么,还是有办法解决?

EDIT 编辑

Found a better implementation with separate images for normal button, selected button and dividers. 找到了更好的实现方式,为普通按钮,选定按钮和分隔线分别设置了图像。 See the answer below. 请参阅下面的答案。

It appears that setting full background image is not the best way to handle custom segmented button. 似乎设置完整的背景图像不是处理自定义分段按钮的最佳方法。 Example found here . 这里找到示例。

Here is the basic code that does everything: 这是完成所有工作的基本代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM