Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove BackgroundView from UITargetedPreview in Swift

Tags:

xcode

swift

I'm trying to remove the background view for my UITargetPreview. I made the background color clear, however, you can still see the frame of the background.

This is what it currently looks like:

I currently have a view that has the text container and the image inside of it and that's what I use as the view for the UITargetedPreview.

Is there a way to only show the image and the text and not the background frame?

like image 926
Sdanson Avatar asked Nov 21 '20 01:11

Sdanson


1 Answers

There is a tricky method to hide the shadow and to do that you should find a view with _UIPlatterSoftShadowView class name in the view hierarchy and then hide it.

func viewByClassName(view: UIView, className: String) -> UIView? {
    let name = NSStringFromClass(type(of: view))
    if name == className {
        return view
    }
    else {
        for subview in view.subviews {
            if let view = viewByClassName(view: subview, className: className) {
                return view
            }
        }
    }
    return nil
}

override func tableView(_ tableView: UITableView, willDisplayContextMenu configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionAnimating?) {
    DispatchQueue.main.async {
        if let window = UIApplication.shared.delegate?.window! {
            if let view = self.viewByClassName(view: window, className: "_UIPlatterSoftShadowView") {
                view.isHidden = true
            }
        }
    }
}

NOTE: It's not documented internal class and can be changed anytime further but it works now on both ios 13/14.

like image 162
iUrii Avatar answered Oct 21 '22 21:10

iUrii