Цвет фона листа действий на iPad - PullRequest
0 голосов
/ 12 января 2020

Я столкнулся со следующей проблемой. Я установил цвет фона листа действий. Для iPhone все работает нормально, но версия для iPad показывает предупреждение без текста (предупреждение полностью заполнено установленным мной цветом). Это ошибка яблока или я что-то не так делаю?

@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

Ответы [ 2 ]

1 голос
/ 12 января 2020

Что не так, так это идея, что вы собираетесь изменить UIAlertController. Он делает то, что делает, и выглядит так, как выглядит, и вы не должны пытаться возиться с этим. Если вам нужно что-то нестандартное, но выглядит и действует как UIAlertController, то создайте его самостоятельно (представленный UIViewController).

0 голосов
/ 12 января 2020

Вы можете написать расширение для UIAlertController, как это.

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")
    }
}
...