Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add transparent present cover view #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

blyscuit
Copy link
Contributor

@blyscuit blyscuit commented Aug 28, 2024

What happened 👀

  • Add PresentCoverTransparentView to present a cover view with transparent background.
  • Update iOS version to 13 to support SwiftUI.
  • Update podspec to 0.1.1.

Insight 📝

  • The new view is meant to be used with FlowStacks.presentCover or UIKit. modalPresentationStyle = .presentCover.

What it does

When presenting a .presentCover screen by FlowStacks, FlowStacks creates a UIHostingView that contains views with background color.

Screenshot 2567-08-28 at 17 21 01

To present a view with no background color, these are set on the views

backgroundColor = .clear
isOpaque = true
Screenshot 2567-08-28 at 17 21 57

Why user .presentCover

.presentCover guaranteed that the view is present above all existing views. To visualize this clearer, alternative are discussed:

  • .overlay: Overlay lives on the current view and does not translate to other views. Overlay is also order dependent when declare
Text("AAA")
.overlay { Text("BBB") }
.overlay { Text("CCC") } // CCC is always on top of BBB
  • .fullScreenCover: Lives on the current view and does not translate to other views. Can have only on active fullScreenCover at a time. Use UIHostingView and require this library.

Proof Of Work 📹

Example usage:

FlowStack($path, withNavigation: true) {
            HomeView()
                .flowDestination(for: Int.self, destination: { number in
                    NumberView(number: number)
                        .frame(width: 200, height: 200)
                        .background(.black.opacity(0.4))
                        .cornerRadius(20)
                        .presentCoverTransparentBackground()
                })
        }
Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-08-28.at.17.03.33.mp4

@blyscuit blyscuit self-assigned this Aug 28, 2024
@blyscuit blyscuit marked this pull request as ready for review August 28, 2024 10:39
@blyscuit blyscuit requested a review from a team August 28, 2024 10:54
Copy link

@ducbm051291 ducbm051291 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest is good to me 👍


public struct PresentCoverTransparentView: UIViewRepresentable {

private let uiHostingViewCount: Int

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name uiHostingViewCount might not clearly convey its purpose. Consider renaming it to something more descriptive

Suggested change
private let uiHostingViewCount: Int
private let maxUIHostingViewsToTraverse: Int: Int: Int

public class PresentCoverTransparentBackgroundView: UIView {

private let uiHostingViewName = "_UIHostingView"
private let uiHostingViewCount: Int

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private let uiHostingViewCount: Int
private let maxUIHostingViewsToTraverse: Int

override public func didMoveToWindow() {
super.didMoveToWindow()
var superview = superview
var traversedUIHostingView = 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var traversedUIHostingView = 0
var traversedCount = 0

super.didMoveToWindow()
var superview = superview
var traversedUIHostingView = 0
while superview != nil && traversedUIHostingView < uiHostingViewCount {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of force unwrapping, you can safely unwrap them using if let or guard let statements.

Suggested change
while superview != nil && traversedUIHostingView < uiHostingViewCount {
while let superview = currentSuperview, traversedCount < maxUIHostingViewsToTraverse {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants