如何为警报 OK 操作按钮提供背景颜色以及如何为 swift 中的警报标题提供顶部 space

How to give background colour for alert OK action button and how to give top space for title in alert in swift

我已经创建了带有图像视图的警报我需要将该图像四舍五入并且我需要为警报完成按钮提供背景颜色。请在代码中帮助我。

这是我的警报代码:

 func showAlert(){

    let alertController = UIAlertController(title:"      Profile created    \n     Sccessfully     ", message: "", preferredStyle: .alert)
    let doneAlert = UIAlertAction(title: "DONE", style: .default, handler: { (action) -> Void in

        self.navigationController?.popViewController(animated: true)
    })
    alertController.addAction(doneAlert)

    image.layer.cornerRadius = image.frame.height/2
    image.layer.borderWidth = 1
    alertController.view.addSubview(image)

    image.translatesAutoresizingMaskIntoConstraints = false
    alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .centerX, relatedBy: .equal, toItem: alertController.view, attribute: .centerX, multiplier: 1, constant: 0))

    alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .top, relatedBy: .equal, toItem: alertController.view, attribute: .top, multiplier: 1, constant: -20))

    alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 40))
    alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 40))
    self.present(alertController, animated: true, completion: nil)

}

我需要这样的提醒

Alert

来自文档:

Important

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

您最好使用实现此功能的现有库,例如https://github.com/vikmeup/SCLAlertView-Swift,或从头开始实施您自己的警报。

您可以使用以下方法。它调整了 UIAlertController


func showConfiguredUIAlertControllerWithTitle(_ title:String, message:String, messageTextAlignment:NSTextAlignment, actionButtonTitle:String, actionViewBackgroundColor color:UIColor, alertTint:UIColor){

        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        //configuring the alert view sans action
        let alertMainSubviews = alert.view.subviews.first?.subviews.first?.subviews.first?.subviews
        let upperView = (alertMainSubviews?.first)! as UIView
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = messageTextAlignment
        let attributedStringTitle = NSAttributedString(string: title, attributes: [NSAttributedString.Key.foregroundColor: UIColor.orange])
        let attributedStringMessage = NSAttributedString(string: "\n" + message, attributes: [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.paragraphStyle: paragraphStyle])
        alert.setValue(attributedStringTitle, forKey: "attributedTitle")
        alert.setValue(attributedStringMessage, forKey: "attributedMessage")

        let line = UIView(frame: CGRect(x: 0, y: 45, width: alert.view.frame.size.width, height: 1.0))
        line.backgroundColor = .black
        upperView.addSubview(line)
        upperView.bringSubviewToFront(line)

        //configuring the action view
        let actionView = (alertMainSubviews?[1])! as UIView
        actionView.backgroundColor = color
        alert.view.tintColor = alertTint

        //adding action to alert
        let action = UIAlertAction(title: actionButtonTitle, style: .default, handler: nil)
        alert.addAction(action)
        self.present(alert, animated: true, completion: nil)
    }

调用如下:

showConfiguredUIAlertControllerWithTitle("Hey There!", message: "I am your friendly neighbourhood Spiderman", messageTextAlignment: .left, actionButtonTitle: "OK", actionViewBackgroundColor: .orange, alertTint: .white)