0

我正在使用以下代码在我的应用程序中自定义所有分段控件。最初我将选定的段设置为索引 2。

在 IOS 6 中一切正常。当我在 IOS5 上测试应用程序时,我意识到分段控制的初始设置有一个错误。选中和未选中状态之间的分离图像设置不正确。因此它看起来像这样。

如果我通过点击 segmentedcontrol 来更改选定的段,则行为正常。这很奇怪。我还应该做些什么来防止这种奇怪的行为?

初始设定

     UISegmentedControl *localSegmentedControl = [[UISegmentedControl alloc] init];

     if ([localSegmentedControl respondsToSelector:@selector(setBackgroundImage:forState:barMetrics:)]) {

    UIImage *segmentUnselectedSelectedDivider       = [UIImage imageNamed:@"segmentedControlSeperatorNS.png"];
    UIImage *segmentSelectedUnselectedDivider       = [UIImage imageNamed:@"segmentedControlSeperatorSN.png"];
    UIImage *segmentUnselectedUnselectedDivider     = [UIImage imageNamed:@"segmentedControlSeperatorNN.png"];

    UIImage *segmentUnselected = [[UIImage imageNamed:@"barButtonPlain.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:0];
    UIImage *segmentSelected = [[UIImage imageNamed:@"doneButton.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:0];


    [[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
                                               forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setBackgroundImage:segmentSelected
                                               forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];


    [[UISegmentedControl appearance] setDividerImage:segmentUnselectedUnselectedDivider
                                 forLeftSegmentState:UIControlStateNormal
                                   rightSegmentState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];

    [[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselectedDivider
                                 forLeftSegmentState:UIControlStateSelected
                                   rightSegmentState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];

    [[UISegmentedControl appearance] setDividerImage:segmentUnselectedSelectedDivider
                                 forLeftSegmentState:UIControlStateNormal
                                   rightSegmentState:UIControlStateSelected
                                          barMetrics:UIBarMetricsDefault];


}
4

3 回答 3

1

在尝试了在 iOS 5 中自定义 UISegmentedControl 中解释的所有相关方法之后

我发现问题与分隔线的宽度有关。http: //www.raywenderlich.com/4344/user-interface-customization-in-ios-5 中有关分段控件自定义的教程假设分隔线图像宽于 2px(在 Retina 中)。我把它们做成了 2px 宽,问题就解决了。

于 2013-03-28T10:17:02.543 回答
0

我认为这是一个已知的错误。这与解决方法相同,可能对您有用。

在 iOS 5 中自定义 UISegmentedControl

于 2013-03-27T15:02:23.027 回答
0

当我使用段控制时,我很累同样的问题。我解决了那个代码

  UIImage *segmentSelected =
[[UIImage imageNamed:@"ikisiSecildiKirmizi.png"]
 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 5, 15)];
UIImage *segmentUnselected =
[[UIImage imageNamed:@"ikisiSecilmediGri.png"]
 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 5, 15)];
UIImage *segmentSelectedUnselected =
[[UIImage imageNamed:@"solSecili.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 5, 15)];
UIImage *segUnselectedSelected =
[[UIImage imageNamed:@"sagSecili.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 5, 15)];

UIImage *segmentUnselectedUnselected =
[[UIImage imageNamed:@"ikisideSecilmemis.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 5, 15)];

[fiyatSaat setBackgroundImage:segmentUnselected
                     forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[fiyatSaat setBackgroundImage:segmentSelected
                     forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

[fiyatSaat setDividerImage:segmentUnselectedUnselected
       forLeftSegmentState:UIControlStateNormal
         rightSegmentState:UIControlStateNormal
                barMetrics:UIBarMetricsDefault];

[fiyatSaat setDividerImage:segmentSelectedUnselected
       forLeftSegmentState:UIControlStateSelected
         rightSegmentState:UIControlStateNormal
                barMetrics:UIBarMetricsDefault];

[fiyatSaat setDividerImage:segUnselectedSelected
       forLeftSegmentState:UIControlStateNormal
         rightSegmentState:UIControlStateSelected
                barMetrics:UIBarMetricsDefault];

NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor blackColor]
                                                       forKey:UITextAttributeTextColor];
[fiyatSaat setTitleTextAttributes:attributes
                         forState:UIControlStateNormal];
NSDictionary *attributes2 = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                                       forKey:UITextAttributeTextColor];
[fiyatSaat setTitleTextAttributes:attributes2
                         forState:UIControlStateHighlighted];
于 2013-03-27T16:08:54.503 回答