2017-06-07 65 views
0

我想通过一些绿色动作来实现视图,并在UIAlertController中使选定的动作变为粗体。而其中一项行动,即解雇按钮必须与其他人分开,并以红色着色。如何在UIAlertAction中显示选定的动作?

我想添加他们的样式.cancel允许显示解雇按钮,但它是大胆的,但绿色。 我该如何做到这一点?

预期的观点:

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

显示你试过代码 –

+0

你可以用图像显示一些预期的结果吗? – nayem

+0

您可以将样式设置为.destructive红色解雇按钮 –

回答

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

试试这个代码。

+0

谢谢,这是我的一个问题,但不是我要求的解决方案之一。我在质疑如何使标题字体重量大胆的一个行动? –

+0

你可以试试这个https://stackoverflow.com/a/36981799/6904341 – gEeKyMiNd

+0

是的,我已经尝试过,但它只适用于UIAlertControllerStyle.alert,并且我正在使用.actionSheet。 –

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

谢谢,这是我的一个问题,但不是我要求的解决方案之一。我在质疑如何使标题字体重量大胆的一个行动? –

+0

UIAlertController的font属性是一个私有属性。建议不要改变这一点,因为如果你改变它,你会违背指导原则。说到这一点,它仍然可以完成。检查此:https://stackoverflow.com/questions/25988639/is-it-possible-to-edit-uialertaction-title-font-size-and-style/27518769#27518769 –

+0

谢谢,但它确实更改字体为每一个动作,但我只需要一个动作。 –

相关问题