iOS - 如何在UIAlertController的新行中添加动作(Swift)

4

我需要在新行中添加操作,就像这样: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。如果操作标题很长,则操作会自动移到新行上。对于较小的操作标题,操作将分组在一行中。有没有什么方法可以做到这一点?


尝试在标题中添加一堆空格: " Yes ". - rmaddy
3个回答

2

没有任何标记能够改变苹果布局其警报按钮的方式。然而,样式为"alert"的UIAlertController会在无法适当地横向布置整个按钮文本字符串时自动将其按钮布局更改为垂直布局。因此,如果按钮的文本足够长,它就会更改布局。

尽管这是您问题的解决方案,我不建议在生产应用程序中随意添加空格或不必要的长字符串到按钮文本中,以便苹果可以垂直布置按钮。这是误用标准组件,并且一旦字符串被本地化到其他语言,它很容易开始出现问题。为了自然地实现所需的行为,我建议创建自己的自定义视图来垂直显示按钮。


1

将这些行从以下内容更改为:

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)

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

如果您使用操作表,请务必为iPad包括以下内容(否则它将崩溃):uncheckInAlert.popoverPresentationController?.barBottonItem = theButtonThatTriggeredTheAlert。这将使弹出窗口与按钮对齐。 - P. Stern

0

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

就像在这段代码中一样

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

1
我尝试过这个。但是这个风格与我需要的设计有点不同。谢谢。 - Oleksandr Yevdokymov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接