The following view is the first row of a List() meant to replace a hidden Navigation Bar.
It features a back navigation button on the left, a centered title in the middle and an optional button in the right side.
The parent view is responsible for the optional right button implementation through an optional @ViewBuilder closure.
If the parent view does not implement any right button, I need to provide a default placeholder frame to ensure the title keeps centered in place.
The problem is how to check if the closure is empty, since comparing it to EmptyView gives following exception:
if self.content == EmptyView() {}
Binary operator '==' cannot be applied to operands of type '() -> Content?' and 'EmptyView'
and comparing it to nil throughs next warning:
if self.content == nil {}
Comparing non-optional value of type '() -> Content?' to 'nil' always returns false
import SwiftUI
struct TestTitle<Content: View>: View {
    @Environment(\.presentationMode) var presentation
    var caption:String
    let content: () -> Content?
    
    public init(_ caption:String, @ViewBuilder content: @escaping () -> Content? = { nil })  {
        self.caption = caption
        self.content = content
    }
    
    var body: some View {
        HStack() {
            Image(systemName: "chevron.backward")
                .onTapGesture {
                    self.presentation.wrappedValue.dismiss()
                }
            
            Spacer()
            Text(self.caption)
            Spacer()
            
            **if self.content == nil** {
                Text(" ")
                    .frame(width:32)
            } else {
                self.content()
            }
        }
    }
}
A possible solution is to compare the Content type against the EmptyView:
if Content.self == EmptyView.self {
    Text(" ")
        .frame(width: 32)
} else {
    self.content()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With