Swift 使用preferredStyle:.ActionSheet更改UIAlertController中取消按钮的颜色

Swift 使用preferredStyle:.ActionSheet更改UIAlertController中取消按钮的颜色,swift,uiactionsheet,uialertcontroller,Swift,Uiactionsheet,Uialertcontroller,有没有可能改变取消按钮的颜色为红色,我知道我们可以使用破坏性的风格 let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Destructive) { action -> Void in print("Cancel") } 但是我想单独使用cancel按钮,就像这样只需将按钮的样式属性设置为破坏性 let cancelAction = UIA

有没有可能改变取消按钮的颜色为红色,我知道我们可以使用破坏性的风格

  let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Destructive) { action -> Void in
            print("Cancel")
        }

但是我想单独使用cancel按钮,就像这样

只需将按钮的样式属性设置为破坏性

let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
            (alert: UIAlertAction!) -> Void in

})

将目标C中的样式从
UIAlertActionStyleDefault
更改为
uiAlertActionStyleDefaulty

UIAlertAction* button = [UIAlertAction actionWithTitle:@"Button title here"
                                      style:UIAlertActionStyleDestructive
                                      handler:^(UIAlertAction * action)
                                      {
                                          // Handle action here....
                                      }];

这是如何像您所说的那样发出警报的代码:

let alert = UIAlertController(title: "Hello", message: "Hello World", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Open in Google Maps", style: . default, handler: nil))
alert.addAction(UIAlertAction(title: "Open in Google", style: . default, handler: nil))
alert.addAction(UIAlertAction(title: "Copy Address", style: . default, handler: nil))

alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
你必须使用两种风格。
在这里,我使用了
.destromical
.default
,它将警报操作分为两部分

Swift 4

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) 
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
您可以使用下面的代码更改
报警操作按钮的
颜色

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")

希望这对您有所帮助。

Swift4.2

如果您有多个
UIAlertAction
,则在
UIAlertController
中添加类似的“取消”
UIAlertAction

 let alert = UIAlertController(title: "Title", message: "Your Message", preferredStyle: UIAlertController.Style.actionSheet)
 alert.addAction(UIAlertAction(title: "first",style: .default, handler: { action in
     //Do something....           
   }))
 alert.addAction(UIAlertAction(title: "second", style: .default, handler: { action in
     //Do something....           
   }))
// Add cancel UIAlertAction
 let cancelAlert = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
     cancelAlert.setValue(UIColor.red, forKey: "titleTextColor")
     alert.addAction(cancelAction).  
     self.present(alert, animated: true, completion: nil)

如果您希望为“取消”按钮实现相同的输出,并且不希望将“取消”按钮类型更改为破坏性。我在代码中使用了cancel类型作为cancel按钮。要实现同样的效果,可以使用以下代码:-

 //MARK:- Function to create the action sheet

  func showAlertSheet(){

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

    // Create Google Map button
    let googleMap = UIAlertAction(title: "Open in Google Maps", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(googleMap)

    // Create Map button
    let map = UIAlertAction(title: "Open in Maps", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(map)

     // Create copy Address button
    let copyAddress = UIAlertAction(title: "Copy Address", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(copyAddress)

    // Create Cancel button
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
        print("Cancel button tapped");
    }
    // Change Cancel title color according to your requirements
    cancelAction.setValue(UIColor.red, forKey: "titleTextColor")

    alertController.addAction(cancelAction)

    // Present Dialog message
    self.present(alertController, animated: true, completion:nil)
}
并且您还可以选择更改“取消”按钮文本颜色。代码的输出如下:-

 //MARK:- Function to create the action sheet

  func showAlertSheet(){

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

    // Create Google Map button
    let googleMap = UIAlertAction(title: "Open in Google Maps", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(googleMap)

    // Create Map button
    let map = UIAlertAction(title: "Open in Maps", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(map)

     // Create copy Address button
    let copyAddress = UIAlertAction(title: "Copy Address", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(copyAddress)

    // Create Cancel button
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
        print("Cancel button tapped");
    }
    // Change Cancel title color according to your requirements
    cancelAction.setValue(UIColor.red, forKey: "titleTextColor")

    alertController.addAction(cancelAction)

    // Present Dialog message
    self.present(alertController, animated: true, completion:nil)
}

此解决方案的安全性如何?如果苹果决定在未来的iOS更新中更改密钥名称,该怎么办?我在应用程序中使用了此解决方案,并在iTunes上成功获得批准。我如何在“取消”按钮中使用自定义字体?。即使我们使用@bikram sapkota,destructive也不会使“取消”按钮分离。默认值与rest操作一起使用。@Nishadaro上述评论是正确的。破坏性不会使取消按钮分离如何更改取消按钮的背景色如何更改取消按钮的背景色?@Miteshjadav问题是关于取消按钮的<代码>着色颜色
反之亦然更改其他按钮的颜色。Reported如何更改取消按钮的背景色?