UIAlertController取消动作背景颜色

时间:2017-07-03 14:26:29

标签: ios swift3

My question is similar to this where I couldn't find the answer.

UIAlertController change background color of Cancel button for action sheetalertController with white borders 我希望带有actionStyle的取消按钮为"取消"并且背景应该是不同的颜色(默认情况下它将是白色)。如何更改“取消”按钮的背景颜色。我不想将actionStyle更改为默认值。有没有其他方法来实现这一目标? 这是我的代码

   func showActionSheet(_ changeAction: UIAlertAction) {
    let alertController = UIAlertController(title: "", message: "Are you sure".localize(), preferredStyle: .actionSheet)
    alertController.view.tintColor = UIColor.green
    if let subview = alertController.view.subviews.first, let alertContentView = subview.subviews.first {
        for innerView in alertContentView.subviews {
            innerView.backgroundColor = UIColor.red
        }
    }
    let cancelAction = UIAlertAction(title: "Cancel".localize(), style: .cancel) { _ in
        self.doneButton.isEnabled = true
    }
    alertController.addAction(changeAction)
    alertController.addAction(cancelAction)
    self.present(alertController, animated: true)
}

1 个答案:

答案 0 :(得分:0)

可以使用UIAppearance和一点点破解来改变它。 请参阅我的回答here