如何使setEnabled在iOS5中的自定义UISegmentedControl上工作?

如何使setEnabled在iOS5中的自定义UISegmentedControl上工作?,ios5,customization,uisegmentedcontrol,Ios5,Customization,Uisegmentedcontrol,我的iPad应用程序中有一个UISegmentedControl,我使用iOS5中提供的新方法对其进行了定制,如下所示: [[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [[UISegmentedControl appearance] setBackgroundImage:segm

我的iPad应用程序中有一个UISegmentedControl,我使用iOS5中提供的新方法对其进行了定制,如下所示:

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

NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], UITextAttributeFont,
                                [UIColor colorWithRed:0.3 green:0.34 blue:0.42 alpha:1], UITextAttributeTextColor,
                                [UIColor whiteColor], UITextAttributeTextShadowColor,
                                CGSizeMake(0, 1), UITextAttributeTextShadowOffset, nil];

[[UISegmentedControl appearance] setTitleTextAttributes:textAttributes forState:UIControlStateNormal];

它看起来很好,工作正常,但是调用setEnabled有一个问题:对任何一个段都没有影响-该段仍然会响应触摸事件。有人知道我需要做些什么来禁用某些段吗?

使用
isEnabledForSegmentAtIndex:
setEnabled:forSegmentAtIndex:
可以在中找到。让我知道这是否有效。

不确定这是否仍然是任何人的问题(因为它在iOS 6中已修复,但以下是我的解决方法(借用另一个问题):

在viewDidLoad中,尝试:

dispatch_async(dispatch_get_main_queue(),^{
    [self.segmentedControl setEnabled:NO forSegmentAtIndex:2];
});

似乎在加载视图时,外观代理的存在会重置UISegmentedControl的其他属性。在主线程上安排此操作将重新启用/重新禁用。顺便说一句,这也适用于选择默认段。

如果注释掉所有外观代码,启用/禁用是否会像预期的那样工作?是,它在没有定制的情况下运行良好。我猜我还需要添加一些其他内容才能使其正常工作。虽然有点离题,但设置段宽度与应用的外观代码不匹配。我只是遇到了定制的uisegmented控件,在setEnabled:forSegmentAtIndex:下表现怪异。有解决方案吗?我可能需要为禁用状态添加自定义覆盖…恐怕我从来没有解决过。我花了几个小时试图让它工作,但最终我不得不使用UIButtons为代码“[self.segmentedControl setEnabled:NO forSegmentAtIndex:0];BOOL isEnabled=[self.segmentedControl isEnabled forSegmentAtIndex:0]重新创建它;'isEnabled设置为否,但当点击段0时控件仍激发其valueChanged操作方法。我刚刚意识到您没有为uicontrol状态禁用设置图像。我不知道这是否是问题的原因,但这样做可能有用。很好。我尝试为所有选项设置图像伊莎贝拉,但它仍然不起作用