Home > Blockchain >  Error when trying to open my Preview in Swiftui
Error when trying to open my Preview in Swiftui

Time:02-24

iam working with swiftui for the first time. iam trying to make a small social network app, so i created a navigation bar which lays over the other views to navigate between them. But when iam trying to creat a Preview a Problem screen pops up, when iam running the app in the simulator everything work fine. enter image description here

enter image description here enter image description here

enter image description here

CodePudding user response:

Your NavigationBar view needs to receive an @EnvironmentObject, also from the preview.

Inside your NavigationBar_Previews, add one more modifier to the view, like this:

NavigationBar()
    .environmentObject(AppInformation())   // Add all initialisers necessary
                                           // to create the instance of AppInformation()

CodePudding user response:

import SwiftUI

struct NavigationBar: View {

@EnvironmentObject var appInfo: AppInformation

var body: some View {
    
    ZStack
    {
Capsule()
    .frame(width: 400, height: 70)
    .foregroundColor(.blue)
    .overlay(
        
        HStack(spacing: 10) {
            ZStack{
                Image(systemName:  self.appInfo.home ? "house.fill" : "house")
                .resizable()
                .frame(width: 30, height: 28)
                .foregroundColor(.white)
                .padding(5)
                
                Button("   ") {
                    appInfo.home = true
                    appInfo.camera = false
                    appInfo.friends = false
                    appInfo.profil = false
                }
                .frame(width: 35, height: 35)
              }
            
            Spacer()
                .frame(width: 30)
            
            ZStack {
                Image(systemName: self.appInfo.camera ? "camera.fill" : "camera")
                    .resizable()
                    .frame(width: 30, height: 25)
                    .foregroundColor(.white)
                    .padding(5)
                
                Button("    ") {
                    appInfo.home = false
                    appInfo.camera = true
                    appInfo.friends = false
                    appInfo.profil = false
                    
                }
                .frame(width: 35, height: 35)
            }
            
            Spacer()
                .frame(width: 30)
            
            ZStack {
                Image(systemName: self.appInfo.friends ? "person.3.fill" : "person.3")
                    .resizable()
                    .frame(width: 35, height: 30)
                    .foregroundColor(.white)
                    .padding()
                
                Button("    ") {
                    appInfo.home = false
                    appInfo.camera = false
                    appInfo.friends = true
                    appInfo.profil = false
                }
                .frame(width: 35, height: 35)
            }
            
            Spacer()
                .frame(width: 30)
            
            ZStack {
                Image(systemName: self.appInfo.profil ? "person.fill" : "person")
                    .resizable()
                    .frame(width: 30, height: 30)
                    .foregroundColor(.white)
                    .padding(5)
                
                Button("    ") {
                    appInfo.home = false
                    appInfo.camera = false
                    appInfo.friends = false
                    appInfo.profil = true
                }
                .frame(width: 35, height: 35)
            }
        }
      )
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
}

}

  • Related