如何在 UIAlertAction 中显示选定的操作?

How to show selected action in UIAlertAction?

我正在尝试通过一些绿色操作来实现视图,并在 UIAlertController 中将所选操作加粗。其中一个动作,即关闭按钮必须与其他动作分开并用红色着色。

我正在尝试使用样式添加它们。取消允许显示关闭按钮,但它是粗体,但是是绿色的。 我如何实现这一目标?

预期观看次数:

我当前的代码:

let alertController
        = UIAlertController(title: nil,
                            message: nil,
                            preferredStyle: .actionSheet)

    let sortByDateAction = UIAlertAction(title: "По дате",
                                         style: .default,
                                         handler: {(action: UIAlertAction!) in
                                            if self.sortBy != "date" {
                                                self.page = 1
                                                self.sortBy = "date"
                                                self.loadTenders()
                                            }
    })
    let moreExpensiveSortAction = UIAlertAction(title: "Дороже",
                                                style: .destructive,
                                                handler: {(action: UIAlertAction!) in
                                                    if self.sortBy != "priceHigh" {
                                                        self.page = 1
                                                        self.sortBy = "priceHigh"
                                                        self.loadTenders()
                                                    }
    })
    let cheaperSortAction = UIAlertAction(title: "Дешевле",
                                          style: .default,
                                          handler: {(action: UIAlertAction!) in
                                            if self.sortBy != "priceLow" {
                                                self.page = 1
                                                self.sortBy = "priceLow"
                                                self.loadTenders()
                                            }
    })

    alertController
        .addAction(sortByDateAction)
    alertController
        .addAction(moreExpensiveSortAction)
    alertController
        .addAction(cheaperSortAction)

    alertController.preferredAction = sortByDateAction

    alertController
        .addAction(UIAlertAction(title: "Dismiss",
                                 style: .cancel,
                                 handler: nil))
let cancelAlert = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler:nil)
        cancelAlert.setValue(UIColor.red, forKey: "titleTextColor")
        alertController.addAction(cancelAlert)

试试这个代码。

操作中文本的颜色 sheet 只不过是视图的色调。根据您的颜色要求进行设置。

此外,要更改取消按钮的字体颜色:设置 titleTextColor 属性 的 UIAlertAction 的值

示例代码如下:

func showCaptureOptions() {

                let actionSheet = UIAlertController(title: "Capture Type", message: "", preferredStyle: .actionSheet)

                let cameraAction = UIAlertAction(title: "Camera", style: .default) { [weak self](action) in
                    self?.initializeImagePicker(captureType: .camera)
                }

                let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default) { [weak self](action) in
                    self?.initializeImagePicker(captureType: .photoLibrary)
                }

                let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
                }

                cancelAction.setValue(UIColor.red, forKey: "titleTextColor"). /// The cancel button will be red

                actionSheet.view.tintColor = UIColor.green /// The action sheet options would be green

                actionSheet.addAction(cameraAction)
                actionSheet.addAction(photoLibraryAction)
                actionSheet.addAction(cancelAction)

                DispatchQueue.main.async {
                    self.present(actionSheet, animated: true, completion: nil)
                }
            }