具有PreferredStyle:.actionSheet的iOS13.1 UIAlertController无法更改标题的文本颜色和字体

时间:2019-09-18 12:21:00

标签: uialertcontroller ios13

我这样更改标题字体和颜色:

let titleAttributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 25)!, NSAttributedStringKey.foregroundColor: UIColor.purple]

alert.setValue(titleString, forKey: "attributedTitle")

在iOS13之前,preferredStyle .alert和.actionSheet都可以正常工作。 现在,它仅适用于.alert,不适用于.actionSheet。

有人需要帮助吗?

1 个答案:

答案 0 :(得分:0)

iOS 13现在将UIAlertController标题嵌入UIVisualEffectView中,并且只有标题的Alpha通道会影响其外观。如果控制精确的颜色至关重要,我认为您可以尝试找到子视图类_UIInterfaceActionGroupHeaderScrollView,删除其子UIVisualEffectView,然后重新添加您自己的UILabel。但是不能保证它会起作用或会成功。触发崩溃。您可以使用下面的allSubviews扩展并将每个扩展与此进行比较:

let grpHeader = NSClassFromString("_UIInterfaceActionGroupHeaderScrollView")

或者如果您只需要确保根据应用的配色方案可以看到标题,那么我在以下方面取得了成功,至少在iOS 14发行之前,使用它应该是相当安全的。

let alertController = UIAlertController(....)
for subView in UIView.allSubviews(of: alertController.view) {
    if let effectView = subView as? UIVisualEffectView {
        if effectView.effect is UIVibrancyEffect {
            if #available(iOS 13.0, *) {
                // iOS 13.1 default blur style is UIBlurEffectStyleSystemVibrantBackgroundRegular which is NOT currently defined anywhere
                // if your alert controller color is dark, set to .systemMaterialDark                                           
                // if your alert controller color is light, set to .systemMaterialLight
                effectView.effect = UIVibrancyEffect(blurEffect: UIBlurEffect(style: UIBlurEffect.Style.systemMaterialDark), style: .secondaryLabel)
            }
            break
        }
    }
}

// You will need this UIView category to get an array of all subviews,
// as the view heirarchy is complex. In iOS13 it is:
// UIAlertController.UIView -> 
//    _UIAlertControllerInterfaceActionGroupView ->
//        UIView
//            _UIInterfaceActionGroupHeaderScrollView
//                ** UIVisualEffectView **
//            _UIInterfaceActionRepresentationsSequenceView
//        _UIDimmingKnockoutBackdropView ]
//            ** all your button actions **

extension UIView {
    class func allSubviews<T : UIView>(of view: UIView) -> [T] {
        var subviews = [T]()

        for subview in view.subviews {
            subviews += allSubviews(of: subview) as [T]

            if let subview = subview as? T {
                subviews.append(subview)
            }
        }

        return subviews
    }
}