You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中实现屏幕录制/截图时自动隐藏UI元素的技术方案求助

Swift: Automatically Hide UI Elements During Screen Recording/Screenshots

Hey there! I’ve tackled this exact requirement in Swift projects before, so let me share a couple of solid, native approaches that don’t depend on third-party libraries like ScreenShield Kit.

Approach 1: Listen to System Notifications

iOS provides built-in notifications for both screenshots and screen recording state changes. You can hook into these to trigger your UI hide/show logic smoothly:

  1. Handle Screenshots: Use UIApplication.userDidTakeScreenshotNotification (triggers right after a screenshot is captured)
  2. Handle Screen Recording: Use UIScreen.capturedDidChangeNotification (triggers when recording starts and stops)

Here’s a code example you can drop into a view controller or your app’s main scene:

import UIKit

class MainViewController: UIViewController {
    // Assume this container holds all UI elements you want to hide
    @IBOutlet weak var mainContentContainer: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupScreenCaptureObservers()
    }
    
    private func setupScreenCaptureObservers() {
        // Listen for screenshot events
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(handleScreenCaptureEvent),
            name: UIApplication.userDidTakeScreenshotNotification,
            object: nil
        )
        
        // Listen for recording state changes
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(handleScreenCaptureEvent),
            name: UIScreen.capturedDidChangeNotification,
            object: nil
        )
    }
    
    @objc private func handleScreenCaptureEvent() {
        let isBeingCaptured = UIScreen.main.isCaptured
        
        // Hide/show your UI (add animation for better UX)
        UIView.animate(withDuration: 0.2) {
            self.mainContentContainer.alpha = isBeingCaptured ? 0 : 1
            self.mainContentContainer.isHidden = isBeingCaptured
        }
        
        // For screenshots: Restore UI after a short delay since the notification fires post-capture
        if !isBeingCaptured && NotificationCenter.default.name(for: UIApplication.userDidTakeScreenshotNotification) != nil {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                self.mainContentContainer.isHidden = false
                self.mainContentContainer.alpha = 1
            }
        }
    }
    
    deinit {
        // Clean up observers to avoid memory leaks
        NotificationCenter.default.removeObserver(self)
    }
}

Approach 2: KVO on UIScreen's isCaptured Property

If you prefer using Key-Value Observing (KVO) instead of notifications, you can observe the isCaptured property of the main screen. This gives you real-time updates when the capture state changes:

import UIKit

class MainViewController: UIViewController {
    @IBOutlet weak var mainContentContainer: UIView!
    private var captureStateObservation: NSKeyValueObservation?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupCaptureStateKVO()
    }
    
    private func setupCaptureStateKVO() {
        captureStateObservation = UIScreen.main.observe(\.isCaptured, options: [.new]) { [weak self] screen, change in
            guard let isCapturing = change.newValue else { return }
            
            UIView.animate(withDuration: 0.2) {
                self?.mainContentContainer.alpha = isCapturing ? 0 : 1
                self?.mainContentContainer.isHidden = isCapturing
            }
        }
    }
    
    deinit {
        // Invalidate the observation to clean up resources
        captureStateObservation?.invalidate()
    }
}

Key Tips for Implementation

  • Global UI Hiding: If you need to hide UI across your entire app, move this logic to your SceneDelegate (or AppDelegate for older projects) and target the app's main window root view.
  • Edge Cases: Test on physical devices—simulator behavior for screen recording/screenshots can differ from real devices.
  • Customization: Instead of hiding the entire container, you can selectively hide sensitive elements (like text fields, buttons) by targeting individual views.

内容的提问来源于stack exchange,提问作者keyra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 05:32:55