UIAlertController:如何使右键加粗?

UIAlertController: How to make the right button bolded?

当我运行下面的代码时,按钮总是在左边,我怎样才能把它改到右边并保持它粗体?

func showInAppPurchaseAlert() {
    let alertController = UIAlertController.init(title: "Upgrade?", message: "Do you want to upgrade to pro version?", preferredStyle: .alert)
    alertController.addAction(UIAlertAction.init(title: "No", style: .default, handler: { action in
        self.dismiss(animated: true, completion: nil)
    }))
    let actionUpgrade = UIAlertAction.init(title: "Yes", style: .cancel, handler: { action in
        self.upgradeToPro()
    })
    alertController.addAction(actionUpgrade)
    alertController.preferredAction = actionUpgrade
    self.present(alertController, animated: true, completion: nil)
}

.default 样式总是在右边,.cancel 样式总是在粗体 文本。为了改变它,您必须像在代码中那样设置 preferredAction,但问题是您在“是”操作上使用了 .cancel,而这正是您想要的右侧和您对 .default 的“否”操作。我试过你的代码,这个版本符合你的要求。

func showInAppPurchaseAlert() {
    let alertController = UIAlertController.init(title: "Upgrade?", message: "Do you want to upgrade to pro version?", preferredStyle: .alert)
    alertController.addAction(UIAlertAction.init(title: "No", style: .cancel, handler: { action in
        self.dismiss(animated: true, completion: nil)
    }))
    
    let actionUpgrade = UIAlertAction.init(title: "Yes", style: .default, handler: { action in
        self.upgradeToPro()
    })
    alertController.addAction(actionUpgrade)

    alertController.preferredAction = actionUpgrade

    self.present(alertController, animated: true, completion: nil)
}

.default 传递给 style:,它将使用取消按钮的外观。