SwiftUI Animation bug? - swift

I'm having this odd issue with macOS Montery 3 & Xcode 13 Beta 3, where I get this error with List's and animations:
[General] Row index 0 out of row range (numberOfRows: 0) for <SwiftUIListCoreOutlineView: 0x133885000>
Its kinda hard to explain, but heres a simple reproduction:
Minimal Reproducible Example
Create a new SwiftUI macOS application
Paste this code:
struct ContentView: View {
#State var items = ["Item"]
#ViewBuilder var mainView: some View {
if items.isEmpty {
Text("Im empty")
}
else {
List(items, id: \.self) {s in
Text(s)
}
}
}
var body: some View {
NavigationView {
mainView
.toolbar {
Button(action: {
withAnimation {
items.removeAll()
}
}) {
Image(systemName: "minus")
}
}
Text("Second")
}
}
}
Run the app, and try resizing the sidebar. (You should be able to)
Then press the minus button on the toolbar. This simply removes all the items.
Then, the resizing of the sidebar should be broken. You might also get a bunch of errors in the console.
Is anybody able to reproduce this issue, and is it a bug in SwiftUI?

got the same behavior. The code works well if I remove the "withAnimation". Or put "items.removeAll()" outside the "withAnimation"

Related

SwiftUI Keyboard dismiss not interactive

Thanks for taking your time to help others :)
Problem description:
App must support iOS 14 (there's no keyboard toolbar), and cannot use Introspect library, sorry.
When using a TextField, I want to dismiss it interactively. It does dismiss the keyboard, but does not take TextField along. And it should.
Simple demo code to replicate what happens:
import SwiftUI
struct ContentView: View {
#State var text: String = ""
init() {
UIScrollView.appearance().keyboardDismissMode = .interactive // To interactively dismiss
}
var body: some View {
VStack {
ScrollView {
LazyVStack {
ForEach(1...200, id: \.self) { msg in
Text("Message \(msg)")
.padding()
.background(Color.red.cornerRadius(8))
}
}
}
TextField("Hello", text: $text)
.textFieldStyle(.roundedBorder)
.padding()
}
.navigationTitle("Example")
.navigationBarTitleDisplayMode(.inline)
}
}
GIF resources to see behaviour:
Good result:
Bad result:
What I have checked?
Registering the keyboard height through Notifications events, like in: this post and adding that height as offset, bottom padding... nothing works.
Any idea?

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())

Menu not updating (SwiftUI bug?)

I'm using macOS Montery Beta 4 along with Xcode 13 Beta 4, and I think I've discovered a bug in SwiftUI.
When using a CommandGroup along with a button that is enabled/disabled based on a condition, the CommandGroup doesn't update. CommandMenu does however.
Reproduction:
Create a new SwiftUI macOS project
Paste the following code in the App file:
class Test: ObservableObject {
#Published var num = 0
}
#main
struct TestApp: App {
#StateObject private var test = Test()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(test)
}
.commands {
CommandGroup(after: .newItem) {
Button(action: {}) {
Text("Test menu item")
}
.disabled(test.num == 0)
}
}
}
}
Paste the following code in the ContentView file:
struct ContentView: View {
#EnvironmentObject var test: Test
var body: some View {
Button(action: {
test.num += 1
}) {
Text(String(test.num) + " ---------")
}
}
}
Run the app, and click on the File menu. You should see that "Test menu item" is disabled as expected.
Click on the button. This simply increments a number by 1. However, "Test menu item" is still disabled, even though test.num != 0.
The thing is, replacing the CommandGroup with CommandMenu("Test menu") fixes it.
All the app does is have a menu item that is disabled if a number is zero. Pressing the button makes that number not zero, but the menu item stays disabled.
Is anybody able to reproduce this, and is this a bug on my part?
a minor variation of #George_E answer, is this:
CommandGroup(after: .newItem) {
TestView(test: test)
}
struct TestView: View {
#ObservedObject var test: Test
var body: some View {
Button(action: {}) {
Text("Test menu item")
}
.disabled(test.num == 0)
}
}
I wouldn't expect that to be intended behaviour. However, I have found a workaround to fix this.
It's odd that onChange(of:perform:) isn't run when test.num changes, but we can still detect the publisher. So I split this into a separate view and when the view receives a new value, the button is updated.
Code:
CommandGroup(after: .newItem) {
TestView(pub: test.$num)
}
struct TestView: View {
let pub: Published<Int>.Publisher
#State private var disabled = true
var body: some View {
Button(action: {}) {
Text("Test menu item")
}
.disabled(disabled)
.onReceive(pub) { new in
disabled = new == 0
}
}
}

How to use #State and #Binding parallel?

My setup:
I am having a ContentView that represents a the length of a final selection (selection.count). Therefore I need a selection variable on my ContentView using a #State propertyWrapper since I want the View to get update as soon as the value changes. The selection is supposed to be made on my SelectionView therefore I am creating a Binding between my selection variables on the ContentView and SelectionView.
My Problem: The UI on my SelectionView is supposed to be updated as well when the selection variable changes but since it is using #Binding and not #State the view does not get updated. So I would need something where I can use a #State and #Binding at the same time or a #Binding which also makes the UI reload.
struct ContentView: View {
#State var selection: [Int] = []
var body: some View {
NavigationView {
Form {
NavigationLink(destination: SelectionView(selection: $selection)) {
Text("Selection: \(selection.count)")
}
}
}
}
}
struct SelectionView: View {
#Binding var selection: [Int]
var body: some View {
NavigationView {
Form {
ForEach((0...9).identified(by: \.self)) { i in
Button(action: {
if self.selection.contains(i) {
self.selection = self.selection.filter { !($0 == i) }
} else {
self.selection.append(i)
}
}) {
if self.selection.contains(i) {
Text("Unselect \(i)")
} else {
Text("Select \(i)")
}
}
}
}
}
}
}
Note: If I am using #State on the SelectionView instead of #Binding it works properly (which obviously requires me to not create the binding which I want).
There's nothing wrong with your binding. It is the right thing to do and it works the way you want it to. A binding is the way you pass mutable state around in SwiftUI, and you are doing that, and it works. A change to a binding does make the view reload.
To convince yourself of that, just get rid of all the extra stuff in your example, and concentrate on the heart of the matter, the binding:
struct ContentView: View {
#State var selection: [Int] = []
var body: some View {
NavigationView {
Form {
NavigationLink(destination: SelectionView(selection: $selection)) {
Text("Selection: \(selection.count)")
}
}
}
}
}
struct SelectionView: View {
#Binding var selection: [Int]
var body: some View {
NavigationView {
VStack {
Button.init("Append") {
self.selection.append(1)
}
Text("Selection: \(selection.count)")
}
}
}
}
Run the example, tap the link, tap the button repeatedly. The display of selection.count is changed in both views. That's what you wanted and that's what happens.
Here's a variant on your original code that displays selection more explicitly (instead of selection.count), and you can see that the right thing is happening:
struct ContentView: View {
#State var selection: [Int] = []
var body: some View {
NavigationView {
Form {
NavigationLink(destination: SelectionView(selection: $selection)) {
Text("Selection: \(String(describing:selection))")
}
}
}
}
}
struct SelectionView: View {
#Binding var selection: [Int]
var body: some View {
NavigationView {
List {
ForEach(0...9, id:\.self) { i in
Button(action: {
if let ix = self.selection.firstIndex(of:i) {
self.selection.remove(at: ix)
} else {
self.selection.append(i)
}
}) {
if self.selection.contains(i) {
Text("Unselect \(i)")
} else {
Text("Select \(i)")
}
}
}
}
}
}
}
The solution to your problem is: upgrade to the latest Xcode.
I tested your code in a playground under Xcode 11 beta 4 and it worked correctly.
I tested your code in a playground under Xcode 11 beta 3 and it failed in the way you describe (I think).
The current version of Xcode 11 as of this answer is beta 5, under which your code doesn't compile because the identified(by:) modifier has been removed. When I changed your code to work under beta 5, by replacing ForEach((0...9).identified(by: \.self)) with ForEach(0...9, id: \.self), it worked correctly.
Therefore I deduce that you are still running beta 2 or beta 3. (Form wasn't available in beta 1 so I know you're not using that version.)
Please understand that, at the moment, SwiftUI is still quite buggy, and also still undergoing incompatible API changes. The bugs are unfortunate, but the API evolution is good. It's better that we suffer a few months of changes now than years of less optimal API later.
This means that you need to try to stay on the latest Xcode 11 beta unless it introduces bugs (like the Path bug in beta 5, if your app uses Path) that prevent you from making any progress.
Thanks to #robmayoff I was able to solve the problem. It was a problem with Xcode 11 Beta 3, installing the newest beta version solved the problem

SwiftUI: NavigationLink not working if not in a List

I guess it might be a bug in beta 3 as the NavigationView is all broken. But a view like that:
struct GenreBadge : View {
#EnvironmentObject var store: Store<AppState>
let genre: Genre
var body: some View {
NavigationLink(destination: MoviesGenreList(genre: genre).environmentObject(store)) {
RoundedBadge(text: genre.name)
}
}
}
is not triggering any push in the navigation stack. The view doens't seems interactive at all. If anyone found a workaround would be good, unless Apple is documenting this behaviour I would consider it broken until beta 4.
There seems to be a bug with the navigation link in Xcode version 11.3(11C29) which I have reported to Apple.
Note: This problem only appears in simulator. It works fine on a real device. Thanks to #djr
The below code works as expect the first time you use the navigation link. Unfortunately it becomes unresponsive the second time around.
import SwiftUI
struct ContentView : View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: SomeView()) {
Text("Hello!")
}
}
}
}
}
struct SomeView: View {
var body: some View {
Text("Detailed View")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Are you actually inside a NavigationView? The following works. But if I missed your point, maybe you can share a little more code.
import SwiftUI
struct ContentView : View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: SomeView()) {
Text("Go!")
}
}
}
}
}
struct SomeView: View {
var body: some View {
Text("Detailed View Here!")
}
}
NavigationLink
A button that triggers a navigation presentation when pressed. This is a replacement for pushViewController
NavigationView {
NavigationLink(destination:
Text("Detail")
.navigationBarTitle(Text("Detail"))
) {
Text("Push")
}.navigationBarTitle(Text("Master"))
}
Or make it more readable by use group destination into it own view DetailView
NavigationView {
NavigationLink(destination: DetailView()) {
Text("Push")
}.navigationBarTitle(Text("Master"))
}
In Xcode 11.3.1, I experienced the same issue but I just had to quit xcode and restart my computer. This apparently fixed an issue (of course its 2021 right now) but I was able to follow apple's swiftui tutorial without any issues. I copied this code and tried it out... worked fine for me. Maybe the issue is your "play" button on the bottom right of your screen was toggled.
Try this:
VStack{
NavigationLink(destination: DetailView()) {
Text("Push")
}.navigationBarTitle(Text("Your Text")).isDetailLink(false)
}
Try this:-
var body: some View {
NavigationView {
NavigationLink(destination: YourView()) {
Text("Go!")
}
}
}