我使用以下代码来设置UISegmentedControl的背景.
homeSegment.setDividerImage(UIImage(named: "seperator.png"),forLeftSegmentState: UIControlState.Normal,rightSegmentState: UIControlState.Normal,barMetrics: UIBarMetrics.Default) homeSegment.setBackgroundImage(UIImage(named: "squareSegment.png"),forState: UIControlState.Selected,barMetrics: UIBarMetrics.Default) homeSegment.setBackgroundImage(UIImage(named: "squareSegment.png"),forState: UIControlState.Normal,barMetrics: UIBarMetrics.Default)
如何设置所选分段的文本颜色?
解决方法
我通过使用setTitleTextAttributes来实现这一目标.下面的示例使用字典来设置属性,因为您最有可能想要同时设置字体类型和大小.尝试将此选定的段文本颜色设置为红色:
let segAttributes: NSDictionary = [ NSForegroundColorAttributeName: UIColor.redColor(),NSFontAttributeName: UIFont(name: "Avenir-MediumOblique",size: 20)! ] homeSegment.setTitleTextAttributes(segAttributes as [NSObject : AnyObject],forState: UIControlState.Selected)
例: