UIAccessibility.isGrayscaleEnabled doesn't work - swift

I was in the process of creating an application in which I use information about Color Filters and more specifically Grayscale. And property UIAccessibility.isGrayscaleEnabled stopped working. I created a simple view in which I check whether the information about bold or color invert works. And it works. Only for grayscale it doesn't. What might be the issue here?
import SwiftUI
import UIKit
struct ContentView: View {
var body: some View {
VStack {
Text("Bold: " + String(UIAccessibility.isBoldTextEnabled))
Text("Grayscale: " + String(UIAccessibility.isGrayscaleEnabled))
Text("Invert: " + String(UIAccessibility.isInvertColorsEnabled))
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I am a beginner and would appreciate any advice ☺️ Thanks!

Related

View Window becomes not transparent - Swift MacOS

I'm working with the latest Swift and want to write a transparent app for Macs. The window should be transparent as I said, also the edges. Does this work at all?
Something like that but that obviously doesn't work...
import SwiftUI
import CoreData
struct ContentView: View {
var body: some View {
Color.clear;
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

SwiftUI | Preview not updating on #Binding var value change

I am learning SwiftUI and tried to make a simple todo list but I'm having issues understanding why #Binding property doesn't update my preview.
The code is the following.
import SwiftUI
struct TodoRow: View {
#Binding var todo: Todo
var body: some View {
HStack {
Button(action: {
todo.completed.toggle()
}, label: {
Image(systemName: todo.completed ? "checkmark.square" : "square")
})
.buttonStyle(.plain)
Text(todo.title)
.strikethrough(todo.completed)
}
}
}
struct TodoRow_Previews: PreviewProvider {
static var previews: some View {
TodoRow(todo: .constant(Todo.sampleData[0]))
}
}
The preview doesn't update when I click the square button but the app works fine. Am I using it incorrectly?
EDIT:
Even without .constant(#), the preview doesn't work.
struct TodoRow_Previews: PreviewProvider {
#State private static var todo = Todo.sampleData[0]
static var previews: some View {
TodoRow(todo: $todo)
}
}
Found a solution in the article Testing SwiftUI Bindings in Xcode Previews.
In order for previews to change you must create a container view that holds state and wraps the view you're working on.
In my case what I've ended up doing was changing my preview to the following.
struct TodoRow_Previews: PreviewProvider {
// A View that simply wraps the real view we're working on
// Its only purpose is to hold state
struct TodoRowContainer: View {
#State private var todo = Todo.sampleData[0]
var body: some View {
TodoRow(todo: $todo)
}
}
static var previews: some View {
Group {
TodoRow(todo: .constant(Todo.sampleData[0]))
.previewDisplayName("Immutable Row")
TodoRowContainer()
.previewDisplayName("Mutable Row")
}
}
}

SwiftUI App works fine in simulator, but not in preview

I'm trying to get preview working in Xcode. Just a simple test app written in Swift. Grabs rows from a Realm database and lists them.
It works fine when I build/run in the simulator but none of the data shows in the ContentView_Preview.
App in Simulator
App in Preview
Here's the code:
import SwiftUI
import RealmSwift
struct HeaderView: View {
var body: some View {
HStack(alignment: .firstTextBaseline) {
Text("Time Spent").font(.largeTitle)
Text("or waisted...").font(.headline)
}
}
}
struct GroupListView: View {
#ObservedResults(TaskGroup.self) var taskGroups
var body: some View {
NavigationView {
// Nothing from here shows in the preview.. but shows fine in the simulator
List(taskGroups) {
Text("Group: " + $0.name)
}
.navigationTitle("Task Groups (\(taskGroups.count))")
// Grrr
}
}
}
struct FooterView: View {
var body: some View {
HStack {
Text("🤬 Grr.. Preview isn't working.")
Spacer()
Text("Simulator works fine though.")
}
}
}
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
HeaderView()
GroupListView()
FooterView()
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I've searched for hours and tried various different suggestions. No luck. Any help would be wonderful.
In your preview it's likely that taskGroups is empty. You could verify this by sticking something like
if taskGroups.isEmpty {
Text("test")
}
For your preview you should inject some placeholder content. Depending on how Realm works there may be a mechanism to do this already, or you may need to create an abstraction that allows you to do this.

How to fix TextField popping back to root on cancel?

I have a simple test watchOS application with a TextField in a secondary view (navigated to from a NavigationLink).
However, when the TextField is canceled or submitted, it will pop back out to the root view instead of staying in the current view. I can't find any information on this anywhere else. Any fixes?
ContentView:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView{
NavigationLink("what", destination: DestinationView())
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
DestinationView:
import SwiftUI
struct DestinationView: View {
#State private var message: String = ""
var body: some View {
TextField(
"Send Something...",
text: $message
)
}
}
struct DestinationView_Previews: PreviewProvider {
static var previews: some View {
DestinationView()
}
}
I found the issue..
I was using a NavigationView, which is deprecated. I removed it and now it's working as intended. (XCode 13.2.1, watchOS 8.3)
*facepalm*

Left animation shift when using nested NavigationLink

I am developing an application using SwiftUI (XCode 12.5.1) and every time one of my View appears after exactly two links of "NavigationLink" everything that is inside a Form is shifted slightly to the left, once the appearing animation is over. The following video shows whats going on : the first two times I open the view, everything is fine. The next two times, when the view is accessed from nested NavigationLink, a slight shift to the left is done once the appearing animation is over.
https://www.dropbox.com/s/k3gjc42xlqp2auf/leftShift.mov?dl=0
I have the same problem on both the simulator and a real device (an iPhone). Here is the project: https://www.dropbox.com/s/l8r5hktg6lz69ob/Bug.zip?dl=0 . The main code is available below.
Here is the main view ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink(destination: PersonView()) {
Text("Person")
}
NavigationLink(destination: IndirectView()) {
Text("Indirect")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Here is the indirect view, IndirectView.swift
import SwiftUI
struct IndirectView: View {
var body: some View {
List {
NavigationLink(destination: PersonView()) {
Text("Person")
}
}
}
}
and the person view, PersonView.swift
import SwiftUI
struct PersonView: View {
var body: some View {
Form {
VStack(alignment: .leading, spacing: 5) {
Text("Last Name")
.font(.system(.subheadline))
.foregroundColor(.secondary)
Text("Fayard")
}
}
}
}
Do you have any idea on what's causing this shift?
Thanks for your help
Francois
Frankly saying I have not idea what causes the problem, but here is the fix: add this line of code no your NavigaitonView
NavigationView {
// everything else
}.navigationViewStyle(StackNavigationViewStyle())