UIAlertAction can't change text color when using addAction

632 Views Asked by At

My alert box is working with a custom background & white text except I can't seem to get white text on my actions Yes & No (from addAction statement). The white text does show use when I use attributed strings for the alert box title & message.

@IBAction func deletePlayer1(sender: UIButton) {
    //set player1Name to nil


    let alert = UIAlertController(title: "Delete Player", message: "Do you want to delete this player?", preferredStyle: .Alert)

    // set font size and color for message in alert box
    let attributedString = NSAttributedString(string: "Do you want to delete this player?", attributes: [
    NSFontAttributeName : UIFont.systemFontOfSize(15),
    NSForegroundColorAttributeName : UIColor.whiteColor()  ])
    alert.setValue(attributedString, forKeyPath: "attributedMessage")

    // set font size and color for title of alert box
    let attributedString1 = NSAttributedString(string: "Delete Player", attributes: [
    NSFontAttributeName : UIFont.systemFontOfSize(15),
    NSForegroundColorAttributeName : UIColor.whiteColor()  ])
    alert.setValue(attributedString1, forKeyPath: "attributedTitle")

    // set the background color and add rounded labels for the alert box
    let subView = alert.view.subviews.first! as UIView
    let contentView = subView.subviews.first! as UIView
    contentView.backgroundColor = UIColor(red: 64.0/255.0, green: 128.0/255.0, blue: 255.0/255.0, alpha: 1.0)
    contentView.layer.cornerRadius = 10


    alert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: {(alertAction: UIAlertAction!) -> Void in

    // set font size and color for title of action text in alert box - this is NOT working
    let attributedString2 = NSAttributedString(string: "Yes", attributes: [
    NSFontAttributeName : UIFont.systemFontOfSize(15),
    NSForegroundColorAttributeName : UIColor.whiteColor()  ])
    alert.setValue(attributedString2, forKeyPath: "attributedTitle")

    print("you press yes")



    }))

    alert.addAction(UIAlertAction(title: "No", style: .Default, handler: {(alertAction: UIAlertAction!) -> Void in

        print("you press no")

    }))

    self.presentViewController(alert, animated: true) { () -> Void in




    }
1

There are 1 best solutions below

1
On

You can get this done by setting the alert's view tintColor to white.

alert.view.tintColor = .whiteColor()