Sunday, December 15, 2024
HomeiOS Developmentswift - The right way to not enable PiP window overlay in...

swift – The right way to not enable PiP window overlay in some areas (like navbar) in iOS apps?


To stop the Image-in-Image (PiP) window from overlaying sure areas in your iOS SwiftUI app, similar to a backside enter field, you should use the smallestUnobscuredContentGuide property out there in iOS 16 and later. This property gives a structure information representing the world not coated by system UI components just like the PiP window.

In SwiftUI; Create an ObservableObject to look at modifications within the unobscured content material space

import SwiftUI

class UnobscuredContentViewModel: ObservableObject {
    @Revealed var unobscuredHeight: CGFloat = 0.0

    init() {
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
            let information = windowScene.smallestUnobscuredContentGuide
            information?.owningView?.layoutIfNeeded()
            unobscuredHeight = information?.layoutFrame.peak ?? 0.0

            NotificationCenter.default.addObserver(
                self,
                selector: #selector(updateUnobscuredHeight),
                identify: UILayoutGuide.layoutFrameDidChangeNotification,
                object: information
            )
        }
    }

    @objc func updateUnobscuredHeight(notification: Notification) {
        if let information = notification.object as? UILayoutGuide {
            self.unobscuredHeight = information.layoutFrame.peak
        }
    }
}

Regulate your SwiftUI view utilizing the noticed unobscured peak

struct ContentView: View {
    @StateObject var viewModel = UnobscuredContentViewModel()

    var physique: some View {
        VStack(spacing: 0) {
            // Your essential content material
            Spacer()
            HStack(alignment: .backside) {
                ComposerInputView(
                    manufacturing unit: DefaultViewFactory.shared,
                    textual content: $viewModel.textual content
                )
            }
            .padding(.backside, viewModel.unobscuredHeight)
        }
    }
}

This method ensures that when the PiP window is lively, your enter field strikes up accordingly, avoiding any overlap.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments