gpt4 book ai didi

ios - 在 iOS 8 上自定义 UISegmentedControl

转载 作者:行者123 更新时间:2023-12-02 09:16:17 25 4
gpt4 key购买 nike

iOS 7 和 8 似乎使自定义 UISegmentedControl 的旧方法不再完全起作用。我一直像过去一样设置分隔线图像和所有内容,但似乎有一些以前的 iOS 版本中不存在的新状态,因此出现了一些奇怪的视觉效果。

enter image description here

这是我正在使用的代码

// Set divider images
self.setDividerImage(UIImage(named: "SegmentedControlNoneSelected"), forLeftSegmentState: .Normal, rightSegmentState: .Normal, barMetrics: .Default)
self.setDividerImage(UIImage(named: "SegmentedControlRightSelected"), forLeftSegmentState: .Normal, rightSegmentState: .Selected, barMetrics: .Default)
self.setDividerImage(UIImage(named: "SegmentedControlRightSelected"), forLeftSegmentState: .Normal, rightSegmentState: .Highlighted, barMetrics: .Default)
self.setDividerImage(UIImage(named: "SegmentedControlLeftSelected"), forLeftSegmentState: .Selected, rightSegmentState: .Normal, barMetrics: .Default)
self.setDividerImage(UIImage(named: "SegmentedControlBothSelected"), forLeftSegmentState: .Selected, rightSegmentState: .Highlighted, barMetrics: .Default)
self.setDividerImage(UIImage(named: "SegmentedControlBothSelected"), forLeftSegmentState: .Selected, rightSegmentState: .Selected, barMetrics: .Default)
self.setDividerImage(UIImage(named: "SegmentedControlBothSelected"), forLeftSegmentState: .Highlighted, rightSegmentState: .Selected, barMetrics: .Default)
self.setDividerImage(UIImage(named: "SegmentedControlBothSelected"), forLeftSegmentState: .Highlighted, rightSegmentState: .Highlighted, barMetrics: .Default)
self.setDividerImage(UIImage(named: "SegmentedControlLeftSelected"), forLeftSegmentState: .Highlighted, rightSegmentState: .Normal, barMetrics: .Default)


// Set background images
var normalBackgroundImage = UIImage(named: "SegmentedControlNormal")
self.setBackgroundImage(normalBackgroundImage, forState: .Normal, barMetrics: .Default)

var selectedBackgroundImage = UIImage(named: "SegmentedControlSelected");
self.setBackgroundImage(selectedBackgroundImage, forState: .Selected, barMetrics: .Default)
self.setBackgroundImage(selectedBackgroundImage, forState: .Highlighted, barMetrics: .Default)
self.setBackgroundImage(selectedBackgroundImage, forState: .Disabled, barMetrics: .Default)
self.setBackgroundImage(selectedBackgroundImage, forState: .Application, barMetrics: .Default)
self.setBackgroundImage(selectedBackgroundImage, forState: .Reserved, barMetrics: .Default)

最佳答案

现在应该处理一些新状态:

//The highlighted button can be still unselected
[self.mySegmentedControl setBackgroundImage:unselectedBackgroundImage
forState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];

//The alredy selected button is stil selected when it is highlighted
[self.mySegmentedControl setBackgroundImage:selectedBackgroundImage
forState:UIControlStateHighlighted|UIControlStateSelected
barMetrics:UIBarMetricsDefault];


//Handle when the right segment is highlighted but the left is selected
[self.mySegmentedControl setDividerImage:leftSelectedImage
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];

//Handle when the right segment is already selected and just highlighted and the right is normal
[self.mySegmentedControl setDividerImage:leftSelectedImage
forLeftSegmentState:UIControlStateHighlighted|UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];

//Handle when the left segment is highlighted but the right is selected
[self.mySegmentedControl setDividerImage:rightSelectedImage
forLeftSegmentState:UIControlStateHighlighted
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];

//Handle when the left segment is already selected and just highlighted and the left is normal
[self.mySegmentedControl setDividerImage:rightSelectedImage
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateHighlighted|UIControlStateSelected
barMetrics:UIBarMetricsDefault];

现在看来,分隔符图像宽度是一个重要的事情。它对内容标签的宽度和位置有影响。因此,deider 图像应该尽可能缩小。

关于ios - 在 iOS 8 上自定义 UISegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26762383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com