UISegmentedControl错误的dividerImage

时间:2013-03-27 14:50:34

标签: ios uiview uiimage uisegmentedcontrol

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

IOS 6中的一切都很完美。虽然我在IOS5上测试应用程序,但我意识到分段控件的初始设置有一个错误。选择和未选择状态之间的分离图像没有设置正确。由于它看起来像这样。

如果我通过点击分段控制来改变所选的段表现正常。这很奇怪。我应该做些什么来防止这种奇怪的行为?

Initial setting

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


}

3 个答案:

答案 0 :(得分:1)

尝试了Customizing UISegmentedControl in iOS 5

中解释的所有相关内容后

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

答案 1 :(得分:0)

我认为这是一个已知的错误。 这里有一个解决方法,可能适合你。

Customizing UISegmentedControl in iOS 5

答案 2 :(得分: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];