Как задать цвет фона для кнопки «ОК» и как сделать верхнее пространство для заголовка в предупреждении в swift - PullRequest
1 голос
/ 03 июня 2019

Я создал оповещение с изображением, мне нужно, чтобы это изображение было округлено, и мне нужно задать цвет фона для кнопки оповещения о готовности. пожалуйста, помогите мне в коде.

вот мой код предупреждения:

 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)

}

Мне нужно предупреждение, как это

Предупреждение

Ответы [ 2 ]

3 голосов
/ 03 июня 2019

Из документации:

Важно

Класс UIAlertController предназначен для использования как есть и не поддерживает подклассы.Иерархия представлений для этого класса является закрытой и не должна быть изменена.

Возможно, вам лучше использовать существующую библиотеку, которая реализует это, например, например, https://github.com/vikmeup/SCLAlertView-Swift, или реализацию собственнойоповещение с нуля.

0 голосов
/ 03 июня 2019

Вы можете использовать следующий метод. Это настраивает 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)

...