简体   繁体   English

iPad 上操作表的背景颜色

[英]Background color of action sheet on iPad

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).对于 iPhone 一切正常,但 iPad 版本显示一个没有任何文本的警报(警报完全填充了我设置的颜色)。 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)
}

在此处输入图片说明 在此处输入图片说明

What's wrong is the idea that you are going to modify a UIAlertController.错误的是您要修改 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).如果你想要一些自定义但看起来和行为像 UIAlertController 的东西,那么你自己做一个(一个呈现的 UIViewController)。

You can write an extension to UIAlertController like this.您可以像这样为 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")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM