iOS - 如何在 UIAlertController 的新行中添加操作 swift

iOS - how to add action in new line in UIAlertConroller swift

我需要在新行中添加操作,就像:alertActionsWithNewLines

我正在尝试此代码:

let uncheckInAlert = UIAlertController(title: "Are you sure you want to uncheck-in?", message: "", preferredStyle: .alert)
    uncheckInAlert.setTitleImage(UIImage(named: "fail_Icon"))
    uncheckInAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) -> Void in
        self.userUncheckIn()
    }))
    uncheckInAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(uncheckInAlert, animated: true, completion: nil)

但结果我有类似的东西:alertActionsInOneLine。 如果动作标题很大,那么动作会自动转移到新行。使用小动作标题,动作被分组在一行中。有没有办法做到这一点?

尝试将样式从 .alert 更改为 .actionSheet

像这段代码

let uncheckInAlert = UIAlertController(title: "Are you sure you want to uncheck-in?", message: "", preferredStyle: .actionSheet)

更改这些行
uncheckInAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

uncheckInAlert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))

let uncheckInAlert = UIAlertController(title: "Are you sure you want to uncheck-in?", message: "", preferredStyle: .alert)

let uncheckInAlert = UIAlertController(title: "Are you sure you want to uncheck-in?", message: "", preferredStyle: .actionSheet)

这将显示 cancel 与其他选项分组的选项。

您无法设置标志来更改 Apple 的警报按钮布局方式。然而,样式为 alertUIAlertController 会在其无法在水平布局中正确匹配按钮文本的整个字符串时自动将其按钮布局更改为垂直布局。所以如果你的按钮的文本足够长,它会改变布局。

虽然这是您问题的解决方案,但我不建议在按钮的文本中添加任意空格或不必要的长字符串,这样 Apple 就会在生产应用程序中垂直布局按钮。它滥用了标准组件,一旦字符串本地化为其他语言,它就很容易开始崩溃。为了自然地实现所需的行为,我建议创建您自己的自定义视图以垂直显示按钮。