Home>

Here to change the UISegmentControl design,

When I wrote this, I got an error.
When you press the error Fix, the following error is displayed in yellow.

Finally, it looks like the following, and even if you check with the actual machine, the text color does not change to white.

I'm having trouble understanding the cause and solution.
Thank you very much.

// Appearance of UISegmentControl
    @IBAction func Segment (_ sender: UISegmentedControl) {
        self.Segment.tintColor = UIColor.clear
        self.Segment.backgroundColor = UIColor (red: 255/255, green: 49/255, blue: 81/255, alpha: 1)
        let attribute = [kCTForegroundColorAttributeName: UIColor.white]
        self.Segment.setTitleTextAttributes (attribute, for: .normal)
    }
  • Answer # 1

    Is it an imagination from a yellow warning, do you use the attribute you made somewhere?
    The part corresponding to the third line of ↓.

    let attribute = [kCTForegroundColorAttributeName: UIColor.white]
    // ↓ Is this description missing?
    Segmented control with outlet connection setTitleTextAttributes (attribute, forState: .Normal)
    I overlooked it because I overlooked it.

    If you change the setting of the initial property of SegmentedControl, Write it in a different place, such as ViewController's viewDidLoad or a lifecycle appropriate place.
    Is it okay because it's tint color?

    Code you want to paste

    ↓ What kind of error do you get when you paste this? I think there is no error just because the color does not change.
    I'm sorry if there is a typo.

    @ IBAction func Segment (_ sender: UISegmentedControl) {
        self.Segment.tintColor = UIColor.clear
        self.Segment.backgroundColor = UIColor (red: 255/255, green: 49/255, blue: 81/255, alpha: 1)
        let attribute = [kCTForegroundColorAttributeName: UIColor.white]
        self.setTitleTextAttributes (attribute, for: .normal)
    }