You can use this extension below. Please apply this method to the parent view
extension UIView { func preventScreenshot(for view: UIView) { let textField = UITextField() textField.isSecureTextEntry = true textField.isUserInteractionEnabled = false guard let hiddenView = textField.layer.sublayers?.first?.delegate as? UIView else { return } hiddenView.subviews.forEach { $0.removeFromSuperview() } hiddenView.translatesAutoresizingMaskIntoConstraints = false self.addSubview(hiddenView) hiddenView.fillSuperview() hiddenView.addSubview(view) } }
So for instance to be able to prevent screenshots on a scrollView
private weak var scrollView: UIScrollView! (it's an outlet)
in your viewDidLoad, just do this below
self.view.preventScreenshot(for: self.scrollView)
Note: fillSuperview is just anchoring your view to its superview so it's like below:
NSLayoutConstraint.activate([ hiddenView.leadingAnchor.constraint(equalTo: self.leadingAnchor), hiddenView.trailingAnchor.constraint(equalTo: self.trailingAnchor), hiddenView.bottomAnchor.constraint(equalTo: self.bottomAnchor), hiddenView.topAnchor.constraint(equalTo: self.topAnchor) ])