如何在UIAlertActionController中为这些按钮设置不同的颜色?

时间:2016-01-10 06:40:15

标签: ios swift uialertcontroller

所以我有一个UIAlertController的实例,我想为每个按钮文本设置不同的颜色。我知道如何将它设置为每个按钮一次但不是每个按钮。有人知道怎么做这个吗?我的代码是吼叫。谢谢!

 func presentAlertController(){
    let alertControl = UIAlertController()
    let sendMessage = UIAlertAction(title: "Send Message", style: UIAlertActionStyle.Default, handler: {
        (alert: UIAlertAction) -> Void in
        print("Send Message")
        self.sendTextMessage()
    })
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: {
        (alert: UIAlertAction) -> Void in
        print("Cancel ")
    })
    alertControl.addAction(sendMessage)
    alertControl.addAction(cancelAction)
    alertControl.view.tintColor = UIColor(red: 8/256.0, green: 148/256.0, blue: 148/256.0, alpha: 1)
    self.presentViewController(alertControl, animated: true, completion: { () -> Void in
        alertControl.view.tintColor = UIColor(red: 8/256.0, green: 148/256.0, blue: 148/256.0, alpha: 1)
    })
}

2 个答案:

答案 0 :(得分:1)

如果您使用原生UIAlertController,则可以这样做。

  

UIAlertView类旨在按原样使用,但不是   支持子类化。此类的视图层次结构是私有的   不得修改。

Reference

您需要自己构建或者可以选择现有的自定义。以下示例:

答案 1 :(得分:0)

这是一种为每个alerviewcontroller按钮设置不同颜色的方法。

#<HTTParty::Response:0x7ff11da769b0 parsed_response={"response"=>{"code"=>"1", "msg"=>"INVALID KEY"}}, @response=#<Net::HTTPUnauthorized 401 Unauthorized readbody=true>, @headers={"server"=>["nginx"], "content-type"=>["text/xml;charset=UTF-8"], "content-length"=>["85"], "content-security-policy-report-only"=>["block-all-mixed-content; report-uri https://track.99acres.com/csp_logging.php;"], "etag"=>["\"d41d8cd98f00b204e9800998ecf8427e\""], "date"=>["Wed, 24 Apr 2019 18:46:46 GMT"], "connection"=>["close"], "set-cookie"=>["99_ab=20; expires=Thu, 23-Apr-2020 18:46:46 GMT; Max-Age=31536000; path=/"]}>