Change default blue color TabBar - swift

When creating a TabBar using TabView and .tabItem, the default colors are: gray when image isn't clicked and blue when it is. Here's a sample code.
struct TabBar: View{
#State var current = 0
var body: some View{
TabView(selection: $current) {
View0()
.tag(0)
.tabItem {
Image(systemName: "anySystemImageName")
Text("")
}
View3()
.tag(1)
.tabItem {
Image(systemName: "anySystemImageName")
Text("")
}
View2()
.tag(2)
.tabItem {
Image(systemName: "anySystemImageName")
Text("")
}
View3()
.tag(3)
.tabItem {
Image(systemName: "anySystemImageName")
Text("")
}
}
}
}
How to use a custom color?

You can use accent color, like
TabView(selection: $current) {
// .. other code
}.accentColor(.red) // << here !!

.accentColor is the keyword to change the color
struct ContentView: View{
#State var current = 0
var body: some View{
TabView(selection: $current) {
Text("View 1")
.tag(0)
.tabItem {
Image(systemName: "circle")
Text("")
}
Text("View 1")
.tag(1)
.tabItem {
Image(systemName: "circle")
Text("")
}
Text("hallo")
.tag(2)
.tabItem {
Image(systemName: "circle")
Text("")
}
Text("hallo")
.tag(3)
.tabItem {
Image(systemName: "circle")
Text("")
}
}.accentColor(.red) //<< here
}
}

Related

How do I get rid of the error 'Argument passed to call that takes no arguments' on my SwiftUI TabView

So I'm trying to make a tab view in the app I'm making to learn SwiftUI but the problem is I get this error saying that I'm passing an argument to a function that takes no arguments. I'll show you my code, I really don't know what's going on and any help is appreciated.
import SwiftUI
struct TabView: View {
var body: some View {
TabView {
Text("The First Tab")
.tabItem {
Image(systemName: "1.square.fill")
Text("First")
}
Text("Another Tab")
.tabItem {
Image(systemName: "2.square.fill")
Text("Second")
}
Text("The Last Tab")
.tabItem {
Image(systemName: "3.square.fill")
Text("Third")
}
}
.font(.headline)
}
}
struct TabView_Previews: PreviewProvider {
static var previews: some View {
TabView()
}
}
I literally copy pasted this example from Apple's website.
Name it something like MyTabView instead. "TabView" is reserved for SwiftUI's TabView.
struct MyTabView: View { /// here!
var body: some View {
TabView {
Text("The First Tab")
.tabItem {
Image(systemName: "1.square.fill")
Text("First")
}
Text("Another Tab")
.tabItem {
Image(systemName: "2.square.fill")
Text("Second")
}
Text("The Last Tab")
.tabItem {
Image(systemName: "3.square.fill")
Text("Third")
}
}
.font(.headline)
}
}
struct MyTabView_Previews: PreviewProvider {
static var previews: some View {
MyTabView()
}
}

tvOS SwiftUI TabView Dynamically Change Bar Color

I have a tvOS App with a TabView across the top. I want to use the background color of the TabView to indicate status. Initially it will be red and when something occurs in one of the views, I want to change the background color of the TabView to green.
I'm using the UITabBar.appearance().barTintColor = UIColor.red in my init() to set the initial color to red, but I can't find a way to change that to green later in execution.
struct ContentView: View {
#State private var selection = 1
init() {
UITabBar.appearance().barTintColor = UIColor.red
}
var body: some View {
TabView (selection:$selection){
Tab1View()
.tabItem {
Image(systemName: "1.square.fill")
Text("Tab 1")
}
.tag(1)
Tab2View()
.tabItem {
Image(systemName: "2.square.fill")
Text("Tab 2")
}.tag(2)
Tab3View()
.tabItem {
Image(systemName: "3.square.fill")
Text("Tab 3")
}.tag(3)
}
.font(.headline)
.accentColor(.white)
.ignoresSafeArea()
}
}
You can use the code below.
import SwiftUI
struct ContentView: View {
#State private var selectedTab = 0
var tabViewTitle = ["First Page","Second Page","Third Page"]
var body: some View {
VStack {
TabView(selection: $selectedTab){
Text(tabViewTitle[0])
.tag(0)
Text(tabViewTitle[1])
.tag(1)
Text(tabViewTitle[2])
.tag(2)
}
.frame(width: 300, height: 500)
.tabViewStyle(PageTabViewStyle())
.background(Color.purple)
.cornerRadius(15)
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
HStack(spacing: 16) {
ForEach(tabViewTitle, id: \.self) { index in
Rectangle()
.fill(self.selectedTab == tabViewTitle.firstIndex(where: {$0 == index}) ? Color.purple : Color.purple.opacity(0.5))
.frame(width: self.selectedTab == tabViewTitle.firstIndex(where: {$0 == index}) ? 16 : 12,height: self.selectedTab == tabViewTitle.firstIndex(where: {$0 == index}) ? 16 : 12)
}
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

TabBar with cases instead of int - SwiftUI

I'm trying to reuse some old code from a question I had previously asked and it's not working anymore, did something happen under the hood?
When I lunch the app the TabBar goes to index 0 instead of 2. This is my current code:
struct iOS_TabBarView: View {
#State private var selectedTab: HostingBarCategories = .Screen3
var body: some View {
TabView(selection: $selectedTab) {
Text("1")
.tag(0)
.tabItem {
Image(systemName: "pencil.and.outline")
Text("1")
}
Text("2")
.tag(1)
.tabItem {
Image(systemName: "checkmark")
Text("2")
}
Text("3")
.tag(2)
.tabItem {
Image(systemName: "calendar.circle.fill")
Text("3")
}
Text("4")
.tag(3)
.tabItem {
Image(systemName: "flame")
Text("4")
}
Text("5")
.tag(3)
.tabItem {
Image(systemName: "slider.horizontal.3")
Text("5")
}
}
}
}
and the HostingBarCategories:
enum HostingBarCategories: Hashable, CaseIterable {
case Screen1
case Screen2
case Screen3
case Screen4
case Screen5
var string: String { String(describing: self) }
}
What's going on?
I assume tag & selection should be the same type, so try
struct iOS_TabBarView: View {
#State private var selectedTab: HostingBarCategories = .Screen3
var body: some View {
TabView(selection: $selectedTab) {
Text("1")
.tag(HostingBarCategories.Screen1) // << like this for all !!

SwiftUI Set Image Boundary

i want to set my right image frame boundaries like the left one without moving image or text. How can I do that. I tried a couple way but I couldn't find the solution.
You should use the built-in TabView. It provides all the functionality for you, and already made with accessibility in mind.
Here is an example (you can change it for your text and images):
struct ContentView: View {
#State private var selection: Int = 1
var body: some View {
TabView(selection: $selection) {
Text("Tab Content 1")
.tabItem {
Label("1", systemImage: "1.square")
}
.tag(1)
Text("Tab Content 2")
.tabItem {
Label("2", systemImage: "2.square")
}
.tag(2)
Text("Tab Content 3")
.tabItem {
Label("3", systemImage: "3.square")
}
.tag(3)
}
}
}
Result:
Custom version (highly unrecommended):
struct ContentView: View {
var body: some View {
VStack {
Spacer()
Text("Main Content")
Spacer()
HStack {
VStack {
Button {
//
} label: {
Label("1", systemImage: "1.square")
}
}.frame(maxWidth: .infinity)
VStack {
Button {
//
} label: {
Label("2", systemImage: "2.square")
}
}.frame(maxWidth: .infinity)
VStack {
Button {
//
} label: {
Label("3", systemImage: "3.square")
}
}.frame(maxWidth: .infinity)
}.frame(height: 50)
}
}
}

TabView doesn't show the next N SelectionValue where SelectionValue == UInt

The code below doesn't show Text("Second View"), because private var selection: UInt.
struct TabMenu: View {
#State private var selection: UInt = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
Image(systemName: "house")
Text("Main")
}
.tag(0)
Text("Second View")
.font(.title)
.tabItem {
Image(systemName: "gear")
Text("Preferences")
}
.tag(1)
}
}
}
In sources of TabView, I found this:
extension TabView where SelectionValue == Int {
#available(watchOS, unavailable)
public init(#ViewBuilder content: () -> Content)
}
How I can create a custom view where SelectionValue == Uint ?
where SelectionValue == Uint
Selection must be the same type as tag is (by default both are Int).
Here is a solution. Tested with Xcode 11.4 / iOS 13.4
struct TabMenu: View {
#State private var selection: UInt = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
Image(systemName: "house")
Text("Main")
}
.tag(UInt(0))
Text("Second View")
.font(.title)
.tabItem {
Image(systemName: "gear")
Text("Preferences")
}
.tag(UInt(1))
}
}
}