UISegmentedControl 分隔线错误

Posted

技术标签:

【中文标题】UISegmentedControl 分隔线错误【英文标题】:UISegmentedControl divider bug 【发布时间】:2013-03-06 17:02:53 【问题描述】:

我的 UISegmentedControl 分隔线遇到了一个奇怪的错误。当我将手指拖开时,该段会丢失其突出显示状态图像,但分隔线会保留它。我需要拖动大约 100 像素才能发生这种情况。

向下,目标:

向下,手指远离目标:

鉴于 0 表示正常状态,1 表示选中状态,2 表示高亮状态;我有所有适当的分隔图像,并为每种情况设置它们,即:

00, 01, 02
10, 11, 12
20, 21, 22

其他一切正常,如果我点击并按住目标没有问题。我不希望用户会点击和拖动,但我宁愿解决这个问题。有没有人经历过这种情况?

【问题讨论】:

可能重复:***.com/questions/9114186/… 对不起,这不是重复的。该人在程序化选择后遇到了分隔符问题。我的分隔线在创建和编程选择时工作,唯一的问题是当按住段,然后将手指拖离点击目标很远时,分隔线会混淆它们的状态。 【参考方案1】:

无需为“11”和“22”状态提供分隔线图像,因为从逻辑上讲,您不能有 2 个相邻的选定段或 2 个相邻的突出显示段。好吧,只有同时点击相邻的片段才会同时突出显示它们,但不太可能。

因此,您应该有以下分隔线图像:

00, 01, 02  
10, --, 12  
20, 21, --

此外,您还应该为所有 3 种不同状态(正常、选定和突出显示)提供 3 张背景图像。这些背景图像的原因以及如何创建它们我已经解释了here(在那个解释中,我只使用了正常和选定状态的 2 个背景图像,但是如果你想使用高亮状态,那么你需要添加第三个背景图片)。

下图显示了这些图像如何在 UISegmentedControl 中使用:

然后使用以下外观方法设置它们:

// Set background images
[segmentedControl setBackgroundImage:background_image_normal
                            forState:UIControlStateNormal
                          barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:background_image_selected
                            forState:UIControlStateSelected
                          barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:background_image_highlighted
                            forState:UIControlStateHighlighted
                          barMetrics:UIBarMetricsDefault];

// Set divider images 0=Normal 1=Selected 2=Highlighted
[segmentedControl setDividerImage:divider_image_00
              forLeftSegmentState:UIControlStateNormal
                rightSegmentState:UIControlStateNormal
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_01
              forLeftSegmentState:UIControlStateNormal
                rightSegmentState:UIControlStateSelected
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_10
              forLeftSegmentState:UIControlStateSelected
                rightSegmentState:UIControlStateNormal
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_02
              forLeftSegmentState:UIControlStateNormal
                rightSegmentState:UIControlStateHighlighted
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_20
              forLeftSegmentState:UIControlStateHighlighted
                rightSegmentState:UIControlStateNormal
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_12
              forLeftSegmentState:UIControlStateSelected
                rightSegmentState:UIControlStateHighlighted
                       barMetrics:UIBarMetricsDefault]
[segmentedControl setDividerImage:divider_image_21
              forLeftSegmentState:UIControlStateHighlighted
                rightSegmentState:UIControlStateSelected
                       barMetrics:UIBarMetricsDefault]

【讨论】:

【参考方案2】:

一种可能的解决方案是手动继承 UISegmentedControl,并覆盖它以确保在调用 UIResponder 事件时正确“清理”分隔符状态。可能是该段预计 touchesMoved 事件将在与初始 touchesBegan 相同的边界内发生。

【讨论】:

【参考方案3】:

别忘了 UIControlStateSelected | UIControlStateHighlighted

// Set background images
UIImage *normalBackgroundImage = [UIImage imageNamed:@"segmented-background-unselected"];
UIImage *selectedBackgroundImage = [UIImage imageNamed:@"segmented-background-selected"];
[self setBackgroundImage:normalBackgroundImage
                forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:selectedBackgroundImage
                forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:normalBackgroundImage
                forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:selectedBackgroundImage
                forState:UIControlStateSelected | UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

// Set divider images
UIImage *buttonImageLeftSelected = [UIImage imageNamed:@"segmented-left-selected"];
UIImage *buttonImageNoneSelected = [UIImage imageNamed:@"segmented-none-selected"];
UIImage *buttonImageRightSelected = [UIImage imageNamed:@"segmented-right-selected"];

[self   setDividerImage:buttonImageNoneSelected
    forLeftSegmentState:UIControlStateNormal
      rightSegmentState:UIControlStateNormal
             barMetrics:UIBarMetricsDefault];

[self   setDividerImage:buttonImageLeftSelected
    forLeftSegmentState:UIControlStateSelected
      rightSegmentState:UIControlStateNormal
             barMetrics:UIBarMetricsDefault];
[self   setDividerImage:buttonImageLeftSelected
    forLeftSegmentState:UIControlStateSelected | UIControlStateHighlighted
      rightSegmentState:UIControlStateNormal
             barMetrics:UIBarMetricsDefault];
[self   setDividerImage:buttonImageLeftSelected
    forLeftSegmentState:UIControlStateSelected
      rightSegmentState:UIControlStateHighlighted
             barMetrics:UIBarMetricsDefault];

[self   setDividerImage:buttonImageRightSelected
    forLeftSegmentState:UIControlStateHighlighted
      rightSegmentState:UIControlStateSelected
             barMetrics:UIBarMetricsDefault];
[self   setDividerImage:buttonImageRightSelected
    forLeftSegmentState:UIControlStateNormal
      rightSegmentState:UIControlStateSelected | UIControlStateHighlighted
             barMetrics:UIBarMetricsDefault];
[self   setDividerImage:buttonImageRightSelected
    forLeftSegmentState:UIControlStateNormal
      rightSegmentState:UIControlStateSelected
             barMetrics:UIBarMetricsDefault];

【讨论】:

以上是关于UISegmentedControl 分隔线错误的主要内容,如果未能解决你的问题,请参考以下文章

UISegmentedControl 错误自定义处理

错位的 UISegmentedControl(潜在错误)

UISegmentedControl去掉背景色与UIScrollView联动

UISegmentedControl 错误的dividerImage

升级到 Xcode 11.x 后出现 UISegmentedControl 错误

错误:点击 UISegmentedControl 时将无法识别的选择器发送到实例