Ios iPad上动作表的背景色

Ios iPad上动作表的背景色,ios,swift,iphone,ipad,actionsheet,Ios,Swift,Iphone,Ipad,Actionsheet,我面临以下问题。 我设置了动作表的背景色。 对于iPhone,一切正常,但iPad版本显示一个没有任何文本的警报警报,警报中完全填充了我设置的颜色。 是苹果病毒还是我做错了什么 @IBAction func save(_ sender: UIButton) { let alert = UIAlertController(title: nil, message: "Error", preferredStyle: .actionSheet) alert.view.tintColor

我面临以下问题。 我设置了动作表的背景色。
对于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)
}

错误的想法是您要修改UIAlertController。它做它做的事,它看起来的样子,你不应该试图弄乱它。如果您想要自定义但外观和行为都像UIAlertController的东西,那么自己制作一个呈现的UIViewController。

您可以像这样编写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")
    }
}

警报完全是用我设置的颜色填充的,但不是你实际显示给我们的任何代码。嗯,我在截图之前发布了代码,或者我误解了你?请稍候。即使你现在真的得到了你想要的样子,它在未来肯定会破裂。