0

I faced the following problem. I set a background color of an action sheet. For iPhone everything works fine, but the iPad version shows an alert without any text in it (alert is completely filled with a color i set). Is it an apple bug or do i do something wrong?

@IBAction func save(_ sender: UIButton) {
    let alert = UIAlertController(title: nil, message: "Error", preferredStyle: .actionSheet)
    alert.view.tintColor = .black
    alert.popoverPresentationController?.sourceView = self.view
    alert.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)

    self.present(alert, animated: true)
}

enter image description hereenter image description here

Artiom
  • 513
  • 1
  • 4
  • 15

2 Answers2

1

What's wrong is the idea that you are going to modify a UIAlertController. It does what it does and looks the way it looks and you should not try to mess with that. If you want something that is custom but looks and acts like a UIAlertController, then make one, yourself (a presented UIViewController).

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

You can write an extension to UIAlertController like this.

extension UIAlertController {

    //Set background color
        func setBackgroundColor(color: UIColor) {
            if let bgView = self.view.subviews.first, let groupView = bgView.subviews.first, let contentView = groupView.subviews.first {
                contentView.backgroundColor = color
            }
        }

//Set title font and title color
    func setTitleColorAndFont(font: UIFont? = UIFont.boldSystemFont(ofSize: 17.0), color: UIColor?) {
        guard let title = self.title else { return }
        let attributeString = NSMutableAttributedString(string: title)
        if let titleFont = font {
            attributeString.addAttributes([NSAttributedString.Key.font: titleFont],
                range: NSRange(location: 0, length: title.utf8.count))
        }
        if let titleColor = color {
            attributeString.addAttributes([NSAttributedString.Key.foregroundColor: titleColor],
                range: NSRange(location: 0, length: title.utf8.count))
        }
        self.setValue(attributeString, forKey: "attributedTitle")
    }

    //Set message font and message color
    func setMessageColorAndFont(font: UIFont? = UIFont.systemFont(ofSize: 13.0), color: UIColor?) {
        guard let message = self.message else { return }
        let attributeString = NSMutableAttributedString(string: message)
        if let messageFont = font {
            attributeString.addAttributes([NSAttributedString.Key.font: messageFont],
                                          range: NSRange(location: 0, length: message.utf8.count))
        }

        if let messageColorColor = color {
            attributeString.addAttributes([NSAttributedString.Key.foregroundColor: messageColorColor],
                                          range: NSRange(location: 0, length: message.utf8.count))
        }
        self.setValue(attributeString, forKey: "attributedMessage")
    }
}
waseemwk
  • 1,499
  • 3
  • 15
  • 44