【问题标题】:Background color of action sheet on iPadiPad上操作表的背景颜色
【发布时间】:2020-01-11 22:56:39
【问题描述】:

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

【问题讨论】:

  • "alert 完全用我设置的颜色填充" 但在您实际展示给我们的任何代码中都没有。
  • 嗯,我在截图之前贴了代码,还是我误解了你?

标签: ios swift iphone ipad actionsheet


【解决方案1】:

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

【讨论】:

  • 第二个。即使你现在看到它想要的样子,它也肯定会在未来打破。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 2015-02-25
    相关资源
    最近更新 更多