ios - UISegmentedControl 和 UIAppearance 的奇怪行为

标签 ios uisegmentedcontrol uiappearance

我在应用程序委托(delegate)中通过这些语句设置分段控件的外观。

    [[UISegmentedControl appearance] setBackgroundImage:[[UIImage imageNamed:@"segmentation_normal.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0 , 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setBackgroundImage:[[UIImage imageNamed:@"segmentation_selected.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0 , 0, 0)] 
        forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

    [[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"segmentation_divider_NormalNormal.png"]   forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"segmentation_divider_NormalSelected.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"segmentation_divider_SelectedNormal.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

结果很好。当我选择任何分割时,背景和分隔线设置正确(应用程序首次启动时默认选择订单分割)。当我选择 Release 部分时,如下所示,一切看起来都很好。

enter image description here

问题是当我离开 View 然后返回 View 时(我存储了选定的索引并将其设置在 viewDidLoad 中以重新选择段),分隔符设置不正确,原因不明。

enter image description here

如果我点击 A-Z 然后点击 Release,它会自行纠正。此错误仅在 View 首次加载且所选段为 Release 或 A-Z 时发生。选择 Order 并加载 View 时,分隔线看起来总是很好。

图片大小:分隔线为 2 像素宽(所有 3 张图片大小相同),背景各为 2 像素宽。

任何想法或指示将不胜感激,在过去的 10 个小时里,我一直在努力寻找解决方案,以解决不可用的问题。

最佳答案

我认为这是 UISegmentedControl 的错误。您可以通过在延迟后设置选定的段来解决它,以允许控件时间自行绘制。你甚至不必使用实际的延迟,就像这样:

[self performSelector:@selector(setPreferences) withObject:nil afterDelay:0.0];

setPreferences 更新分段控件的值。

关于ios - UISegmentedControl 和 UIAppearance 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11183519/

相关文章:

ios - `AFNetworking` 需要 CocoaPods `Podfile`

ios - 我怎么知道在ios中打开或关闭飞行模式

iOS:如何使用 FQL 从 Facebook 获取事件(开始时间 > 今天)?

ios - UISegmentedControl 粘滞模式

iphone - iOS 将 UISegmentedControl 选择存储在 NSUserDefaults 中

ios - 分段控制: controlling repeat interval

objective-c - UIAppearance 对 Label 文本颜色没有影响

ios - 使用具有 50 条记录限制的 FILO 记录插入来执行 RealmSwift

ios - UISearchBar完全自定义背景

ios - 使用 UIAppearance 为 UITableViewCell 文本设置样式