SegmentedControl和UIAppearance的奇怪

SegmentedControl和UIAppearance的奇怪

本文介绍了UISegmentedControl和UIAppearance的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过app委托中的这些语句设置分段控件的外观。

I am setting the appearance of the segmented control via these statements in the app 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];

结果没问题。当我选择任何段时,背景和分隔线设置正确(当应用程序首次启动时,默认情况下选择了订单段)。如下所示,当我选择发布细分时,一切看起来都不错。

The result is fine. Backgrounds and dividers are set correctly when I select any of the segments (the Order segment is selected by Default when the app first launches). When I select the Release segment as you can see below everything looks fine.

问题是当我离开视图然后回到视图时(我存储选定的索引并将其设置为viewDidLoad重新选择片段),由于某种未知原因,分隔符设置不正确。

The problem is when I go out of the view then back to the view (I store the selected index and set it in the viewDidLoad to reselect the segment), the divider is set incorrectly for some unknown reason.

如果我点击AZ然后发布,它会自行更正。此错误仅在视图首次加载且选定的段为Release或A-Z时发生。选择Order并加载视图时,分隔符总是看起来很好。

If I tap on A-Z then Release, it corrects itself. This bug only occurs when the view first loads and the selected segment was either Release or A-Z. The divider always looks fine when Order is selected and the view loads.

图像大小:分隔符宽2px(所有3个图像大小相同),背景为每个2 px宽。

Image sizes: the divider is 2px wide (all 3 images are the same size), the backgrounds are 2 px wide each.

任何想法或指示都会受到高度赞赏,过去10个小时我一直在努力寻找无法获得的解决方案。

Any ideas or pointers would be highly appreciated, I have been pulling my hair for the past 10 hours trying to find a solution to no available.

推荐答案

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

This is a bug with UISegmentedControl, I think. You can work around it by setting the selected segment after a delay, to allow the control time to draw itself. You don't even have to use an actual delay, just something like:

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

其中 setPreferences 更新分段的值控制。

Where setPreferences updates the value of your segmented control.

这篇关于UISegmentedControl和UIAppearance的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:52