crashoverride777 crashoverride777 - 1 month ago 18
Swift Question

Why is UIPreviewActionStyle destructive title not red?

I am implementing 3D Touch Peek and pop preview actions in my application.

Here is what I have in my ViewController that gets used for the peek and pop.

var previewActions: [UIPreviewAction] {

let item1 = UIPreviewAction(title: "Item1", style: .default) { (action, vc) in
// run item 1 action
}

let item2 = UIPreviewAction(title: "Item2", style: .destructive) { (action, vc) in
// run item 2 action
}

return [item1, item2]
}


The actions are working correctly but the destructive action (item 2) is not displaying the title in red, its still blue.

How can I get the title to display in red like the apple photos app does for the delete button?

I thought the behaviour is the same as UIAlertControllers where destructive is red.

Answer

Try to use UIPreviewActionItem instead.

@available(iOS 9.0, *)
override var previewActionItems: [UIPreviewActionItem] {

    let item1 = UIPreviewAction(title: "Item1", style: .default) {
        (action, vc) in
        // run item 1 action
    }

    let item2 = UIPreviewAction(title: "Item2", style: .destructive) {
        (action, vc) in
        // run item 2 action
    }

    return [item1, item2]
}
Comments