You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UIAlertController无障碍测试出现标题、消息及按钮警告问题求助

无障碍测试中UIAlertController触发辅助功能检查器警告的问题

我在项目中多处使用UIAlertController,每次无障碍测试弹出警告框时,辅助功能检查器(Accessibility Inspector)都会针对标题、消息和按钮发出警告。

以下是相关代码:

@objc func showCustomAlert() {
    prepareAlert(title: "How would you like to login?", message: "gmail, facebook, apple, twitter, mobile ,github, yahoo")
}

func prepareAlert(title:String, message:String) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    
    alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { _ in
        alert.dismiss(animated: true)
    }))
    
    present(alert, animated: true)
}

func showAlert(title:String, message:String) {
    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
    
    //customize the title
    let titleString = title
    let titleFont = UIFont.preferredFont(forTextStyle: .headline)
        let titleMetrics = UIFontMetrics(forTextStyle: .headline)
        let scaledTitleFont = titleMetrics.scaledFont(for: titleFont)
    
    let titleAttributes: [NSAttributedString.Key: Any] = [
        .font: scaledTitleFont, // Custom font and size
        .foregroundColor: UIColor.green            // Custom color
    ]
    let attributedTitle = NSAttributedString(string: titleString, attributes: titleAttributes)
    alert.setValue(attributedTitle, forKey: "attributedTitle")
    
    //customize the message
    let messageString = message
    let messageFont = UIFont.preferredFont(forTextStyle: .body)
        let messageMetrics = UIFontMetrics(forTextStyle: .body)
        let scaledMessageFont = messageMetrics.scaledFont(for: messageFont)
    let messageAttributes: [NSAttributedString.Key: Any] = [
        .font: scaledMessageFont, // Custom font and size
        .foregroundColor: UIColor.blue,
        
    ]
    let attributedMessage = NSAttributedString(string: messageString, attributes: messageAttributes)
    alert.setValue(attributedMessage, forKey: "attributedMessage")
    
    alert.addAction(UIAlertAction(title: "Ok", style: .default,handler: { _ in
        alert.dismiss(animated: true)
    }))
    
    self.present(alert, animated: true)
}

我尝试了上述两种(带字体自定义和不带字体自定义的)实现方法,但问题仍然存在。

内容的提问来源于stack exchange,提问作者Mallikharjun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 08:27:40