0

我遇到了以下问题。我设置了操作表的背景颜色。对于 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)
}

在此处输入图像描述在此处输入图像描述

4

2 回答 2

1

问题是您要修改 UIAlertController 的想法。它做它所做的事情,看起来就像它看起来的样子,你不应该试图弄乱它。如果您想要一些自定义但看起来和行为像 UIAlertController 的东西,那么自己制作一个(呈现的 UIViewController)。

于 2020-01-12T00:16:54.540 回答
0

您可以像这样为 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")
    }
}
于 2020-01-12T07:43:01.480 回答