UISegmentedControl 错误的dividerImage [英] UISegmentedControl wrong dividerImage
问题描述
我正在使用以下代码自定义应用程序中的所有分段控件.最初我将所选分段设置为索引 2.
I am custumizing all segmented controls in my app with the following code.Initially I set the selected segment to index 2.
在IOS 6中一切正常.我在IOS5上测试应用程序时,我发现分段控件的初始设置有一个错误.选中和未选中状态之间的分离图像设置不正确.因此看起来像这样.
Everything works perfect in IOS 6.While I was testing the app on IOS5, I realised that the initial setting of segmented control had a bug.The separation image between selected and unselected state is not set right.Due to that it looks like this.
如果我通过点击segmentedcontrol来改变选定的片段,它的行为是正常的.这很奇怪.我还应该做些什么来防止这种奇怪的行为?
If i change the selected segments by tapping the segmentedcontrol behaves normal.It is very weird.What elsse shall i do to prevent this strange behaviour?
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];
}
推荐答案
在尝试了 在 iOS 5 中自定义 UISegmentedControl
我发现问题与分隔线的宽度有关.http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5 假设分离图像的宽度大于 2px(在视网膜).我把它们做成 2px 宽,问题就解决了.
i figured out that the problem is related with the width of the separation line.The tutorial about segmented control customisation in http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5 assumes that the separation images are wider than 2px (in Retina). I made them exactly 2px wide and the problem is resolved.
这篇关于UISegmentedControl 错误的dividerImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!