Reputation: 193
I am trying to create a tabbed application in Swift using Swift UI. I am using @ObservedObject to store the current view name that should be rendered.
I am trying to change the background color of the UI Tab Bar however every time I add the init to override the method begins chucking errors such as "Argument passed to call that takes no arguments".
I have attached a snippet of the controller code below. Any help to resolve this issue is much appreciated.
import SwiftUI
struct ContentView: View {
@State private var selection = 0
@ObservedObject var coreRouter: CoreRouter
init() {
UITabBar.appearance().backgroundColor = UIColor.blue
}
var body: some View {
TabView(selection: $selection){
Text("First Screen")
.font(.title)
.tabItem {
VStack {
Image(systemName: "house")
Text("Home")
}
}
.tag(0)
Text("Second View")
.font(.title)
.tabItem {
VStack {
Image("calendar")
Text("New Items")
}
}
.tag(1)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(coreRouter: CoreRouter())
}
}
Upvotes: 1
Views: 3084
Reputation: 114875
Since your ContentView
has a coreRouter
property, your init
needs to accept a value to assign to that property;
struct ContentView: View {
@State private var selection = 0
@ObservedObject var coreRouter: CoreRouter
init(coreRouter: CoreRouter) {
self.coreRouter = coreRouter
UITabBar.appearance().backgroundColor = UIColor.blue
}
}
Upvotes: 6
Reputation: 8091
try this: (i had to create a dummy class, because you did not provide the code for corerouter)
class CoreRouter : ObservableObject {
@Published var a = false
}
struct ContentView: View {
@State private var selection = 0
@ObservedObject var coreRouter: CoreRouter
init() {
coreRouter = CoreRouter()
UITabBar.appearance().backgroundColor = UIColor.blue
}
var body: some View {
TabView(selection: $selection){
Text("First Screen")
.font(.title)
.tabItem {
VStack {
Image(systemName: "house")
Text("Home")
}
}
.tag(0)
Text("Second View")
.font(.title)
.tabItem {
VStack {
Image("calendar")
Text("New Items")
}
}
.tag(1)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Upvotes: -1