iOS13.1 UIAlertController with preferedStyle: .actionSheet cannot change title's text color and font

元气小坏坏 提交于 2020-03-05 04:25:26

问题


I change title font and color like this:

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

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

Before iOS13 this worked fine both for preferredStyle .alert and .actionSheet. Now it only works for .alert and doesn't work for .actionSheet.

Someone please any help?


回答1:


iOS 13 now embeds the UIAlertController title in a UIVisualEffectView, and only the title's alpha channel affects its appearance. If it is critical to control the exact color, I think you could try finding the subview class _UIInterfaceActionGroupHeaderScrollView, remove its child UIVisualEffectView, and then add your own UILabel back in. But no guarantees it will work or that it won't trigger a crash. You can use the allSubviews extension below and compare each to this:

let grpHeader = NSClassFromString("_UIInterfaceActionGroupHeaderScrollView")

Or if you just need to ensure that your title is visible based on your app's color scheme I had success with the following, which should be quite safe to use at least until iOS 14 is released.

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
    }
}


来源:https://stackoverflow.com/questions/57992692/ios13-1-uialertcontroller-with-preferedstyle-actionsheet-cannot-change-titles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!