Selected text state in a segmented control in Xcode

I am working on my first application using Swift and Xcode.

In one of my views, I have a segmented control that contains emoticons as display text. However, I have a problem, when one of the emoticons is selected, it becomes darkened, as shown in the image below:

segmentedControl

I think the problem is with the text attributes of the selected state of my segmented control, and my attempt to fix set the text attributes of the selected state in the same way as the text attributes of the normal state

override func viewDidAppear(animated: Bool) { moodSelector.setTitleTextAttributes(moodSelector.titleTextAttributesForState(.Normal), forState: .Selected) } 

However, this does not work. Any help would be appreciated?

+7
ios cocoa-touch xcode swift uisegmentedcontrol
source share
1 answer

You can set NSAttributedStringKey.foregroundColor (formerly known as NSForegroundColorAttributeName ) to any color, and this will prevent this behavior. For example, in Swift 3 or 4:

 override func viewDidLoad() { super.viewDidLoad() // whether `viewDidLoad` or `viewDidAppear`, make sure to call the appropriate `super` method, too let attributes = [NSAttributedStringKey.foregroundColor : UIColor.white] moodSelector.setTitleTextAttributes(attributes, for: .selected) } 

Yielding:

segmented control with emojis

+4
source share

All Articles