iOS 8上的错误图像选择了UISegmenetedControl按

时间:2015-01-30 22:05:22

标签: ios swift ios8 uisegmentedcontrol

我有一个带有2个按钮的UISegmentedControl。我为.Normal.Selected.Highlighted设置了背景图片。一切都在iOS 7中完美运行,但在iOS8中几乎完美无缺。当您正常使用按钮时,只需点击未选中的按钮,一切正常。但出于某种原因,在iOS 8中按下已经选择的按钮时,您将获得灰色背景而不是所选图像。以下是显示我的意思的图片。

选择了右侧

Regular Tabs, with one selected

左侧点击未选中的标签

enter image description here

右侧选中了右侧

enter image description here

我已经尝试了一些方法,比如设置backgroundColor来清除,或者看看是否存在我缺少的状态,但我似乎无法找到解决方案。

感谢您的任何建议。

这里要求的是代码:

self.feedTypeButton.setBackgroundImage(UIImage.imageWithColor(UIColor(hex: "#cee4ee")).resizableImageWithCapInsets(UIEdgeInsetsZero), forState: .Normal, barMetrics: .Default)
self.feedTypeButton.setBackgroundImage(UIImage.imageWithColor(Colors.blue).resizableImageWithCapInsets(UIEdgeInsetsZero), forState: .Selected, barMetrics: .Default)
self.feedTypeButton.setBackgroundImage(UIImage.imageWithColor(Colors.mediumBlue).resizableImageWithCapInsets(UIEdgeInsetsZero), forState: .Highlighted, barMetrics: .Default)

var dividerImage = UIImage.imageWithColor(Colors.blue, size: CGSize(width: 1, height: 28)).resizableImageWithCapInsets(UIEdgeInsetsZero)
self.feedTypeButton.setDividerImage(dividerImage, forLeftSegmentState: .Selected, rightSegmentState: .Normal, barMetrics: .Default)
self.feedTypeButton.setDividerImage(dividerImage, forLeftSegmentState: .Normal, rightSegmentState: .Selected, barMetrics: .Default)
self.feedTypeButton.setDividerImage(dividerImage, forLeftSegmentState: .Normal, rightSegmentState: .Normal, barMetrics: .Default)


self.feedTypeButton.setTitleTextAttributes([ NSForegroundColorAttributeName: Colors.gray, NSFontAttributeName: UI.regularFontOfSize(13) ], forState: .Normal)
self.feedTypeButton.setTitleTextAttributes([ NSForegroundColorAttributeName: Colors.white, NSFontAttributeName: UI.boldFontOfSize(15)], forState: .Selected)
self.feedTypeButton.setTitleTextAttributes([ NSForegroundColorAttributeName: Colors.white ], forState: .Highlighted)

1 个答案:

答案 0 :(得分:0)

在选择已选择的段时段变为灰色的原因是因为分段控件缺少选定状态并同时突出显示。

在您的情况下,要解决问题,请致电:

self.feedTypeButton.setBackgroundImage(UIImage.imageWithColor(Colors.mediumBlue).resizableImageWithCapInsets(UIEdgeInsetsZero), forState:.Selected | .Highlighted, barMetrics: .Default)