Change Status Bar Color SwiftUI no UIHosting - ios

I'm trying to change my status bar color dynamically without success, anyone has any idea how to change it ?
I'm not using UIHosting controller so there is no AppDelegate or SceneDelegate fully using swiftUI:
#main
struct MyProjectApp: App{}
If i set ZStack {}.preferredColorScheme(.light) it only apply once, so if i go to another view and try to put ZStack {}.preferredColorScheme(.dark) it doesn't work.
I think i have try all the question here on stackoverflow, so if anyone have a definitive solution i appreciated.
#Updated code example
struct ContentA: View {
var body: some View {
NavigationView {
ZStack {
NavigationLink(destination: ContentB()) {
Text("Show Detail View")
}.navigationBarTitle("Navigation")
}.preferredColorScheme(.light)
}
}
}
struct ContentB: View {
var body: some View {
ZStack {
Text("Hello ContetB")
.padding()
.foregroundColor(.green)
}.preferredColorScheme(.dark)
}
}
If you try to change StatusBar color using this way it doesn't work.

The colors within the app should be dynamic automatically, unless you set them specifically. You can toggle the .preferredColorScheme within the Previews section.
struct ContentView: View {
var body: some View {
Text("Hello, world!")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.preferredColorScheme(.dark)
}
}

Related

View background stretches over the safe area when using GeomtryReader

When having a view inside a GeomtryReader and setting the background to a color. It ignores the vertical safe area and stretches beyond it. Only when the view is at the vertical edges.
It doesn't only happen on Preview, it happens on actual device as well.
I am not sure if this is a bug or a normal behaviour of SwiftUI if so what am I doing wrong?
I have a very simple view that you can test with as follows:
import SwiftUI
struct TestView: View {
var body: some View {
GeometryReader { reader in
Text("TEST")
.background(Color.red)
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
You can add it in a VStack and add a Spacer and you will see the same behaviour when it is at the bottom.
import SwiftUI
struct TestView: View {
var body: some View {
GeometryReader { reader in
VStack {
Spacer()
Text("TEST")
.background(Color.red)
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
I can clip it by adding .clipped() to the Text and it will be removed but this sounds like a work around to me.
I stepped away from SwiftUI for a while so I do not remember if this is a normal behaviour or not. I did some research but couldn't find anything related to this.
I am using XCode 14.2 (14C18) and Swift swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)

SwiftUI NavigationLink Item foregroundColor not working

Im trying to use SwiftUI List & NavigationLink with custom font/colors/view
Nothing is working I tried setting the .foregroundColor and using other View customized yet i cant change the color of the text, i only want to have a black text color nothing fancy
struct SettingsView: View {
var body: some View {
List{
Section(header: Text("North America")){
NavigationLink(destination: CASettingsView() ) {
SettingItemView(value: "USA")
}.foregroundColor(.red)
NavigationLink(
destination: Text("Destination"),
label: {
Text("Click Here!")
.foregroundColor(.black)
}
)
}
}
.listStyle(.plain)
}
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
CASettingsView()
}
}
Your NavigationLinks are greyed out because they aren’t embedded in a NavigationView. Wrap your whole list in NavigationView { }
In my case it works.
NavigationView {
NavigationLink(destination: Empty()) {
Button("Smth", action: {})
}.foregroundColor(.black)
}
You just need to use foregroundColor to NavigationLink instead of Text()

How can I declare the correct content to import another swiftui file into the contentview file?

I'm a bit new to swift and am a bit confused about what is being asked of me. Some background: I found a guy online who has created a swiftui file for a sliding bottom view bar and I wanted to implement it into my ContentView swiftui file. I have looked up and down the internet and tried to play around with things after clicking "fix" in xcode but I have had no luck.
Here are images of the error, me clicking "fix" prompt from xcode, and the BottomSheetView.swift file's code -> https://imgur.com/a/GqEjMzo
Can someone help me by explaining what exactly swift is asking me to do and let me know how I can solve this error?
Here is my code:
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack{
VStack{
MapView()
.edgesIgnoringSafeArea(.all)
let heightDouble = CGFloat(150.00)
BottomSheetView(isOpen: .constant(true), maxHeight: heightDouble, content: <#() -> Content#>)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
Errors in SwiftUI are not always shown where they really are. The problem in your code snippet is at the line:
let heightDouble = CGFloat(150.00)
and it should be: Closure containing a declaration cannot be used with function builder 'ViewBuilder'
Try this and you'll see, that the problem is not in other views:
var body: some View {
ZStack{
VStack{
Text("hello") // replaced MapView()
.edgesIgnoringSafeArea(.all)
// you need to delete this row to avoid error
let heightDouble = CGFloat(150.00)
Text("world") // replaced BottomSheetView(...)
}
}
}
the code in body variable must return some View, but defining a constant inside it violates this rule
update
in BottomSheetView you have content which is other View. I don't see all the code, but I think it should be something like this:
struct ContentView: View {
#State var isOpen = true
var body: some View {
ZStack{
VStack{
MapView()
.edgesIgnoringSafeArea(.all)
BottomSheetView(isOpen: self.$isOpen, maxHeight: CGFLoat(150)) {
Text("bottom")
}
}
}
}

Why SwiftUI view transitions modifiers don't work?

I needed to add a view to my scene using a view transition. I was trying to use the modifier .transition(.opacity) but it just adds the view without any transition. I also tried the other transition types and they don't work. Since this is a really simple test project a don't see where's the issue. Here's a link to a video of my problem
import SwiftUI
struct Test: View {
#State var show = false
var body: some View {
VStack {
Button(action: {self.show.toggle()}) {
Text("Button")
}
if show {
Text("Test Text")
.transition(.move(edge: .top))
}
}
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Test()
}
}
You need to add animation for the transition
if show {
Text("Test Text").animation(.linear)
.transition(.move(edge: .top))
}

How to change NavigationViewStyle from a descendent view in SwiftUI?

I have a NavigationView at the root of my app that has the initial state of .navigationViewStyle(.stack), but as I navigate, I want to change that style to .doubleColumn.
From my descendent view I have tried calling navigationViewStyle(.doubleColumn) much like how you change the navigation bar title, but no luck.
Trying a ternary operator in the root view also doesn't work navigationViewStyle(isDoubleColumn ? .doubleColumn : .stack)
ROOT VIEW:
var body: some View {
NavigationView {
VStack {
//stuff
}
}
.navigationViewStyle(.stack)
}
Descendent View
var body: some View {
ScrollView{
//stuff
}
.navigationViewStyle(.doubleColumn) //doesn't work
}
DoubleColumnNavigationViewStyle is a struct that needs to be initialized. Try it this way:
.navigationViewStyle(DoubleColumnNavigationViewStyle())
The code below does the job but with the price of losing of some state data so it may not really be the solution.
struct SwiftUIView: View {
#State var isDoubleColumn = false
var body: some View {
Group {
if isDoubleColumn{
NavigationView {
Text("Hello, World!")
}
.navigationViewStyle(DoubleColumnNavigationViewStyle())
} else {
NavigationView {
Text("Hello, World!")
}
}
}
}
}
I'm not an expert but something tells me that the style can't be changed during NavigationView lifetime.

Resources