SwiftUI: App crashes when resizing ScrollView with 3 TextField inside HStack on macOS
Here is workaround. Fixed variant tested with Xcode 11.4 / macOS 10.15.4
struct CrashedContentView: View {
var body: some View {
GeometryReader { gp in
ScrollView {
HStack {
ForEach(1...3, id: \.self) { idx in
TextField("", text: .constant("text \(idx)"))
}
}.frame(width: gp.size.width)
}
}
}
}
SwiftUI List crashes when removing a selected item
Re-tested after installing MacOS 11.0 Beta 7 (20A5374g).
The example code doesn’t crash any more, the bug seems to be fixed.
Thanks to all for testing and so giving me the hint, that it's a MacOS beta bug. :-)
SwiftUI holding reference to deleted core data object causing crash
I basically had the same issue. It seems that SwiftUI loads every view immediately, so the view has been loaded with the Properties of the existing CoreData Object. If you delete it within the View where some data is accessed via @ObservedObject, it will crash.
My Workaround:
- The Delete Action - postponed, but ended via Notification Center
Button(action: {
//Send Message that the Item should be deleted
NotificationCenter.default.post(name: .didSelectDeleteDItem, object: nil)
//Navigate to a view where the CoreDate Object isn't made available via a property wrapper
self.presentationMode.wrappedValue.dismiss()
})
{Text("Delete Item")}
You need to define a Notification.name, like:
extension Notification.Name {
static var didSelectDeleteItem: Notification.Name {
return Notification.Name("Delete Item")
}
}
- On the appropriate View, lookout for the Delete Message
// Receive Message that the Disease should be deleted
.onReceive(NotificationCenter.default.publisher(for: .didSelectDeleteDisease)) {_ in
//1: Dismiss the View (IF It also contains Data from the Item!!)
self.presentationMode.wrappedValue.dismiss()
//2: Start deleting Disease - AFTER view has been dismissed
DispatchQueue.main.asyncAfter(deadline: .now() + TimeInterval(1)) {self.dataStorage.deleteDisease(id: self.diseaseDetail.id)}
}
- Be safe on your Views where some CoreData elements are accessed - Check for isFault!
VStack{
//Important: Only display text if the disease item is available!!!!
if !diseaseDetail.isFault {
Text (self.diseaseDetail.text)
} else { EmptyView() }
}
A little bit hacky, but this works for me.
TextField with animation crashes app and looses focus
- How can I keep the focus of the TextField after the body is
recalculated because of a change in state?
You have two of them. Two different TextField could not be in editing state at the same time.
The approach suggested by Asperi is the only possible.
The reason, why your code crash is not easy explain, but expected in current SwiftUI.
You have to understand, that Group is not a standard container, it just like a "block" on which you can apply some modifiers. Removing Group and using wraping body in ViewBuilder
struct CustomTextFieldView: View {
@Binding var text: String
@State var editing: Bool = false
@ViewBuilder
var body: some View {
if self.editing {
TextField("", text: $text)
.background(Color.red)
.onTapGesture {
self.editing.toggle()
}
} else {
ZStack(alignment: .leading) {
TextField("", text: $text)
.background(Color.green)
.onTapGesture {
self.editing.toggle()
}
}
}
}
}
the code will stop to crash, but there is other issue, the keyboard will dismiss immediately. That is due the tap gesture applied.
So, believe or not, you have to use ONE TextField ONLY.
struct CustomTextFieldView: View {
@Binding var text: String
@State var editing = false
var textField: some View {
TextField("", text: $text, onEditingChanged: { edit in
self.editing = edit
})
}
var body: some View {
textField.background(editing ? Color.green : Color.red)
}
}
Use this custom text field elsewhere in your code, as you want
Related Topics
Swift: Simple Dispatchqueue Does Not Run & Notify Correctly
Problems with Unified Logging, Staticstring, Customstringconvertible and Description
How to Change The Default Skscene That Displays on Startup
How to Pass a Completion Block to Another Class in Swift
Swift 3/4 Dash to Camel Case (Snake to Camelcase)
What Is The Reason to Store Subscription into a Subscriptions Set
How to Query Firebase Data Childbyautoid
How to Set a Custom Annotations for All Points Except for User Location
Lazy Initialization and Deinit
Parse Codable Classes and Avoid Repetition
Reload Tableview After Clicking The Button Present Inside One of The Tableview Cell in Swift
Pass and Print All Cases in an Enum in Swift
Swift "Print" Doesn't Appear in Stdout But 3Rd Party C Library Logs Do When Running in Docker on Ecs
Bar Button Item Tint Color Not Working
Why Do I Get Rgb Values of (0,0,0) for an Nsimage with a Transparent Background
How to Get Address in English Language Only Using Gmsgeocoder