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

Wrong position of toast while displaying in sheet #5

Closed
Rminsh opened this issue Mar 8, 2021 · 6 comments
Closed

Wrong position of toast while displaying in sheet #5

Rminsh opened this issue Mar 8, 2021 · 6 comments
Labels
bug Something isn't working

Comments

@Rminsh
Copy link

Rminsh commented Mar 8, 2021

Describe the bug
Toast will show in wrong position with hud display mode when using it in sheet. This bug appears only in iOS

To Reproduce
Steps to reproduce the behavior:

  1. Define a view as sheet
  2. Add toast to it with hud display mode
  3. Call the toast to show

Expected behavior
The Y Position of Toast should be much more below or even below the NavigationView (if is defined)

Screenshots
IMG_0361

Smartphone:

  • Device: iPhone 12
  • OS: iOS 14.4

Sample code:

import SwiftUI
import AlertToast

struct ContentView: View {
    
    @State private var showingDetail = false
    
    var body: some View {
        Button("Show Detail") {
            showingDetail.toggle()
        }
        .sheet(isPresented: $showingDetail) {
            NextView()
        }
    }
}

struct NextView: View {
    
    @State var showToast = false
    
    var body: some View {
        ScrollView {
            Button("Show Detail") {
                showToast.toggle()
            }
            .padding()
        }.toast(isPresenting: $showToast, duration: 3, tapToDismiss: true, alert: {
            AlertToast(
                displayMode: .hud,
                type: .systemImage("checkmark.circle.fill", .green),
                title: "Test",
                subTitle: "Some message")
            
        })
    }
}
@Rminsh Rminsh added the bug Something isn't working label Mar 8, 2021
@elai950
Copy link
Owner

elai950 commented Mar 8, 2021

The .toast modifier should always be at the top of all view builders.
Add a view builder (for example VStack) then implement the .toast on the vstack after the .sheet modifier.
Try this:

var body: some View {
        VStack{
        Button("Show Detail") {
            showingDetail.toggle()
        }
        }
.sheet(isPresented: $showingDetail) {
            NextView()
        }
//Implement `.toast` here
    }

@Rminsh
Copy link
Author

Rminsh commented Mar 8, 2021

The .toast modifier should always be at the top of all view builders.
Add a view builder (for example VStack) then implement the .toast on the vstack after the .sheet modifier.
Try this:

var body: some View {
        VStack{
        Button("Show Detail") {
            showingDetail.toggle()
        }
        }
.sheet(isPresented: $showingDetail) {
            NextView()
        }
//Implement `.toast` here
    }

If I implement toast modifier in the Parent View, the toast will show up behind my sheet like this:
Screen Shot 2021-03-08 at 4 00 15 PM

import SwiftUI
import AlertToast

struct ContentView: View {
    
    @State var showToast = false
    @State private var showingDetail = false
    
    var body: some View {
        Button("Show Detail") {
            showingDetail.toggle()
        }
        .sheet(isPresented: $showingDetail) {
            childView
        }
        .toast(isPresenting: $showToast, duration: 3, tapToDismiss: true, alert: {
            AlertToast(
                displayMode: .hud,
                type: .systemImage("checkmark.circle.fill", .green),
                title: "Test",
                subTitle: "Some message")
            
        })
    }
    
    var childView: some View {
        ScrollView {
            Button("Show Detail") {
                showToast.toggle()
            }
            .padding()
        }
    }
}

So the correct way is to implement toast in the Child view and the Y Position should be based on the current view

@elai950
Copy link
Owner

elai950 commented Mar 8, 2021

This is actually how AlertToast works. By receiving the Y position in the current view.
Do not put '.toast' on Button, implement it on ViewBuilder, like Group.

'.sheet' can be on a button but it's not the same case for AlertToast.

@0xifarouk
Copy link

This is actually how AlertToast works. By receiving the Y position in the current view.
Do not put '.toast' on Button, implement it on ViewBuilder, like Group.

'.sheet' can be on a button but it's not the same case for AlertToast.

This doesn't solve the problem, I have put the toast above the VStack in a Sheet, but the position still incorrect as @Rminsh showed.

@iKK001
Copy link

iKK001 commented Jun 16, 2021

...same here, this does not solve the problem (iOS 14.6)

It seems that .hud has a problem.

As Rminsh wrote "...to implement toast in the Child view..." works but unfortunately not for .hud.

What is the solution for .sheet and the need for a .hud type alert ??

@elai950
Copy link
Owner

elai950 commented Jul 21, 2021

I've released a new version where you can manually offset the height until I'll find a better solution.

.toast(isPresenting: ...., offsetY: CGFloat)

Closing issue.

@elai950 elai950 closed this as completed Jul 21, 2021
ekurutepe pushed a commit to ekurutepe/AlertToast that referenced this issue Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants