UISegmentedControl 错误的dividerImage
Posted
技术标签:
【中文标题】UISegmentedControl 错误的dividerImage【英文标题】:UISegmentedControl wrong dividerImage 【发布时间】:2013-03-27 14:50:33 【问题描述】:我正在使用以下代码在我的应用程序中自定义所有分段控件。最初我将选定的分段设置为索引 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];
【问题讨论】:
Weird behavior with UISegmentedControl and UIAppearance 的可能重复项 - 我的回答有一个可行的解决方法。 【参考方案1】:在尝试了Customizing UISegmentedControl in iOS 5中解释的所有相关方法后
我发现问题与分隔线的宽度有关。http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5 中有关分段控件自定义的教程假设分隔图像宽于 2px(在 Retina 中)。 我将它们精确地设置为 2px 宽,问题就解决了。
【讨论】:
【参考方案2】:我认为这是一个已知的错误。 在这里也有一个解决方法,可能对你有用。
Customizing UISegmentedControl in iOS 5
【讨论】:
【参考方案3】:当我使用段控制时,我对同样的问题感到非常厌倦。我解决了那个代码
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];
【讨论】:
你的段背景和分隔线的大小是多少? 分段背景和分隔线的尺寸相同,33x34 用于视网膜显示以上是关于UISegmentedControl 错误的dividerImage的主要内容,如果未能解决你的问题,请参考以下文章
UISegmentedControl 错误的dividerImage
UIToolbar 内的 UISegmentedControl 的横向高度确实错误
升级到 Xcode 11.x 后出现 UISegmentedControl 错误
错误:点击 UISegmentedControl 时将无法识别的选择器发送到实例
当在两个布局之间快速切换使用 UISegmentedControl 时,我得到一个线程 1 exc_bad_access code=2 错误