Reputation: 45
I have a Zstack within ContentView in xcode I'm attempting to add a background image to. This image is intended to serve as the background for the entire screen, but it does not show up. The image is saved in xcassets with the correct label for the code below. This has been double, no triple-checked.
My code:
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@State var phoneNumberCompleted = false;
@State var phoneNumber = ""
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
animation: .default)
private var items: FetchedResults<Item>
var body: some View
{
ZStack
{
NavigationView
{
Form
{
Section(header: Text("Account Credentials"))
{
TextField("Phone Number", text: $phoneNumber)
if phoneNumberCompleted
{
Button(action: {
}) {
HStack
{
Spacer()
Text("Log in")
.font(.system(size: 20))
.padding(2)
.background(Color.red)
.foregroundColor(Color.white)
.cornerRadius(10)
.shadow(color: Color.gray, radius: 2)
Spacer()
}
}
}
}
Section(header: Text("ABOUT APP"))
{
HStack
{
Text("Version")
Spacer()
Text("0.0.1")
}
}
}
.navigationBarTitle("Welcome to Mapwork")
}
}.background(Image("bg"))
.opacity(100)
}
}
I'm wondering if I am using the correct approach or if something is wrong here. Any help appreciated.
Upvotes: 1
Views: 282
Reputation: 958
First of all you need to add the code below to make the background clear:
init() {
UITableView.appearance().backgroundColor = .clear
}
Then you can add your background without a problem and here it is the result:
.navigationBarTitle("Welcome to Mapwork")
.background(Image("image1"))
Note: I don't know why are you using opacity
anyway, for your information the opacity value it must be between 0
and 1
.
Upvotes: 2