UIAlertController имеет различную структуру DOM в iPhone и iPad
Я показываю
UIAlertController
в iOS
и тот же код выполняется для iPhone and iPad
, Так может кто-нибудь помочь с этим. Как я Google, но не нашел ничего, чтобы оправдать это. Ниже приведен код, который я использовал:
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: текст "Удалить все сохраненные" подпадает под UIAView структуры DOM при выполнении автоматического тестирования для UIAlertController в iPhone. iPad: текст "Удалить все сохраненные" подпадает под UIView структуры DOM при выполнении автоматического тестирования для UIAlertController в iPad.
Так почему же возникает разница в структуре UIAlertController, поскольку iPhone iPad показывает UIView и iPhone UIAView для одного и того же кода.
1 ответ
Вы должны добавить контроллер popoverpresentation для 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)
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)
}