首页 文章

UIAlertController在iPhone和iPad中具有不同的DOM结构

提问于
浏览 1185
-1

我在 iOS 中显示 UIAlertController ,并且 iPhone and iPad 执行相同的代码 . 所以任何人都可以帮助这个 . 正如我谷歌但没有找到任何证明这一点的事情 . 以下是我使用过的代码: -

let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Button.Ok".localizedValue(), style: .destructive, handler: { _ in

})
let cancelAction = UIAlertAction(title: "Button.Cancel".localizedValue(), style: .default, handler: { _ in
    alert.dismiss(animated: true, completion: nil)
})
alert.accessibilityLabel = self.viewModel.deleteUserConfirmationAccessibilityId()
alert.addAction(okAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)

所以问题是这样的: - iPhone:当为iPhone中的UIAlertController进行自动化测试时,“删除所有已保存的”文本来自DOM结构的UIAView . iPad:在iPad中对UIAlertController进行自动化测试时,“删除所有已保存的”文本属于DOM结构的UIView .

那么为什么UIAlertController结构会出现差异,因为iPhone iPad会显示相同代码的UIView和iPhone UIAView .

1 回答

  • 0

    你必须为ipad添加popoverpresentation控制器 . 请看下面的代码

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
    let okAction = UIAlertAction(title: "Button.Ok".localizedValue(), style: .destructive, handler: { _ in
    
    })
    let cancelAction = UIAlertAction(title: "Button.Cancel".localizedValue(), style: .default, handler: { _ in
    alert.dismiss(animated: true, completion: nil)
    })
    alert.accessibilityLabel = 
    self.viewModel.deleteUserConfirmationAccessibilityId()
    alert.addAction(okAction)
    alert.addAction(cancelAction)
    //self.present(alert, animated: true, completion: nil)
    
    
    if(UIDevice.current.userInterfaceIdiom == .pad){
            // for iPAD support:
            alert.popoverPresentationController?.sourceView = self.view
            alert.popoverPresentationController?.sourceRect = CGRect(x:self.view.bounds.width / 2.0, y:150, width:1.0, height:1.0)
            self.present(actionSheet, animated: true, completion: nil)
    }else{
            // for iPHONE support:
            let rootViewController: UIViewController = (UIApplication.shared.keyWindow?.rootViewController)!
            rootViewController.present(alert, animated: true, completion: nil)
    }
    

相关问题