如何在UIAlertAction中显示所选操作?

时间:2017-06-07 06:17:21

标签: ios swift uialertcontroller

我正在尝试使用一些绿色操作来实现视图,并在UIAlertController中将所选操作设置为粗体。其中一个动作,即解除按钮必须与其他动作分开并用红色着色。

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

预期观点:

expected view

我目前的代码:

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))

2 个答案:

答案 0 :(得分:2)

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

试试这段代码。

答案 1 :(得分:1)

操作表中文本的颜色只是视图的色调。根据您的颜色要求设置它。

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

以下是示例代码:

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)
                }
            }