UIAlertController:如何使右侧按钮加粗?

RRN*_*RRN 5 ios swift uialertcontroller

当我运行以下代码时,按钮总是在左侧,如何将其更改为右侧并保持粗体

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)
}
Run Code Online (Sandbox Code Playgroud)

Nic*_*apu 8

样式.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)
}
Run Code Online (Sandbox Code Playgroud)