Search code examples
iosswiftxcodeswiftuixcode11

How can I declare the correct content to import another swiftui file into the contentview file?


I'm a bit new to swift and am a bit confused about what is being asked of me. Some background: I found a guy online who has created a swiftui file for a sliding bottom view bar and I wanted to implement it into my ContentView swiftui file. I have looked up and down the internet and tried to play around with things after clicking "fix" in xcode but I have had no luck.

Here are images of the error, me clicking "fix" prompt from xcode, and the BottomSheetView.swift file's code -> https://i.sstatic.net/ufNdQ.jpg

Can someone help me by explaining what exactly swift is asking me to do and let me know how I can solve this error?

Here is my code:

import SwiftUI

struct ContentView: View {
    var body: some View {

        ZStack{
        VStack{
            MapView()
                .edgesIgnoringSafeArea(.all)

            let heightDouble = CGFloat(150.00)

            BottomSheetView(isOpen: .constant(true), maxHeight: heightDouble, content: <#() -> Content#>)


        }
    }
}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
}

Solution

  • Errors in SwiftUI are not always shown where they really are. The problem in your code snippet is at the line:

    let heightDouble = CGFloat(150.00)
    

    and it should be: Closure containing a declaration cannot be used with function builder 'ViewBuilder'

    Try this and you'll see, that the problem is not in other views:

        var body: some View {
    
            ZStack{
                VStack{
                    Text("hello") // replaced MapView()
                        .edgesIgnoringSafeArea(.all)
                    // you need to delete this row to avoid error
                    let heightDouble = CGFloat(150.00)
    
                    Text("world") // replaced BottomSheetView(...)
    
                }
            }
        }
    

    the code in body variable must return some View, but defining a constant inside it violates this rule

    update in BottomSheetView you have content which is other View. I don't see all the code, but I think it should be something like this:

    struct ContentView: View {
    
        @State var isOpen = true
        var body: some View {
    
            ZStack{
            VStack{
                MapView()
                    .edgesIgnoringSafeArea(.all)
    
                BottomSheetView(isOpen: self.$isOpen, maxHeight: CGFLoat(150)) {
                    Text("bottom")
                }
    
    
            }
        }
    }