Demystify SwiftUI

RSS for tag

Discuss the WWDC21 session Demystify SwiftUI.

Posts under wwdc21-10022 tag

10 Posts

Post

Replies

Boosts

Views

Activity

Conditionals and Identity
When using conditionals in view bodies, can I preserve identity between the true and false sides of the conditional by adding an explicit id? struct DogTreat: Identifiable { var expirationDate: Date var serialID: String var id: String { serialID } } ... struct WrapperView: View { ... var treat: DogTreat var isExpired: Bool { treat.expirationDate < .now } var body: some View { if isExpired { DogTreatView(treat) .id(treat.id) .opacity(0.75) else { DogTreatView(treat) .id(treat.id) } } ... } Does this perform / behave the same as struct WrapperView: View { ... var treat: DogTreat var isExpired: Bool { treat.expirationDate < .now } var body: some View { DogTreatView(treat) .opacity(isExpired ? 0.75 : 1.0) } ... }
2
0
1.9k
May ’22
View body is called although its @Binding has not changed
SwiftUI promise is to call View’s body only when needed to avoid invalidating views whose State has not changed. However, there are some cases when this promise is not kept and the View is updated even though its state has not changed. Example: struct InsideView: View { @Binding var value: Int // … } Looking at that view, we’d expect that its body is called when the value changes. However, this is not always true and it depends on how that binding is passed to the view. When the view is created this way, everything works as expected and InsideView is not updated when value hasn’t changed. @State private var value: Int = 0 InsideView(value: $value) In the example below, InsideView will be incorrectly updated even when value has not changed. It will be updated whenever its container is updated too. var customBinding: Binding<Int> { Binding<Int> { 100 } set: { _ in } } InsideView(value: customBinding) Can anyone explain this and say whether it's expected? Is there any way to avoid this behaviour that can ultimately lead to performance issues? Here's a sample project if anyone wants to play with it: import SwiftUI struct ContentView: View { @State private var tab = 0 @State private var count = 0 @State private var someValue: Int = 100 var customBinding: Binding<Int> { Binding<Int> { 100 } set: { _ in } } var body: some View { VStack { Picker("Tab", selection: $tab) { Text("@Binding from @State").tag(0) Text("Custom @Binding").tag(1) } .pickerStyle(SegmentedPickerStyle()) VStack(spacing: 10) { if tab == 0 { Text("When you tap a button, a view below should not be updated. That's a desired behaviour.") InsideView(value: $someValue) } else if tab == 1 { Text("When you tap a button, a view below will be updated (its background color will be set to random value to indicate this). This is unexpected because the view State has not changed.") InsideView(value: customBinding) } } .frame(width: 250, height: 150) Button("Tap! Count: \(count)") { count += 1 } } .frame(width: 300, height: 350) .padding() } } struct InsideView: View { @Binding var value: Int var body: some View { print("[⚠️] InsideView body.") return VStack { Text("I'm a child view. My body should be called only once.") .multilineTextAlignment(.center) Text("Value: \(value)") } .background(Color.random) } } extension ShapeStyle where Self == Color { static var random: Color { Color( red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1) ) } }
2
0
2.1k
Dec ’21
Swift in XCode
Hi there, I am trying to upload my AR experience from Reality Composer into XCode using Swift. Although I have followed the documentation from Apple's presentation, minute 40:53, (https://developer.apple.com/videos/play/wwdc2019/609/?time=2453) of working with AR and XCode, I receive an error message every time I try to load my scene, as the load prompt seems to become part of the scene title and my title itself is not recognized. For example: Module '...' has no member named 'load...'. If anyone can help me here, I would be very grateful.
0
0
472
Oct ’21
How to set UIScreen.main.brightness in SwiftUI
I have a current app in the store written in Objective C that works OK, but I want to update it via SwiftUI as a universal app. In my original app, I adjust the display brightness for night time use by setting UIScreen.main.brightness = 0.2 for example. When exiting the application, I return it to its previous value. I have been unable to find out how this can be done in SwiftUI. Anyone know?
0
0
928
Aug ’21
Conditionals and Identity
When using conditionals in view bodies, can I preserve identity between the true and false sides of the conditional by adding an explicit id? struct DogTreat: Identifiable { var expirationDate: Date var serialID: String var id: String { serialID } } ... struct WrapperView: View { ... var treat: DogTreat var isExpired: Bool { treat.expirationDate < .now } var body: some View { if isExpired { DogTreatView(treat) .id(treat.id) .opacity(0.75) else { DogTreatView(treat) .id(treat.id) } } ... } Does this perform / behave the same as struct WrapperView: View { ... var treat: DogTreat var isExpired: Bool { treat.expirationDate < .now } var body: some View { DogTreatView(treat) .opacity(isExpired ? 0.75 : 1.0) } ... }
Replies
2
Boosts
0
Views
1.9k
Activity
May ’22
Creating Children Interactive book in Swift UI or Playground
Could you please show me an example of a complete project that shows how to create Children Interactive books in Swift UI or Playground with links to various pages of the book that i can follow step-by-step?
Replies
0
Boosts
0
Views
664
Activity
Mar ’22
LandmarkRow.swift Cannot Preview in This File
I'm following the Swift Tutorial, and everything is working ok until I create the LandmarkRow.swift I keep getting the "Cannot Preview in This File" error, even after copying and pasting the code from the tutorial. Any ideas?
Replies
2
Boosts
0
Views
646
Activity
Feb ’22
SwiftUI ScrollView and LazyVStack issue
Bouncing from top to bottom won’t work properly. It’s working only when load some content more than screen height size… iOS14! But, on iOS 15 all work like created
Replies
0
Boosts
0
Views
1.1k
Activity
Feb ’22
View body is called although its @Binding has not changed
SwiftUI promise is to call View’s body only when needed to avoid invalidating views whose State has not changed. However, there are some cases when this promise is not kept and the View is updated even though its state has not changed. Example: struct InsideView: View { @Binding var value: Int // … } Looking at that view, we’d expect that its body is called when the value changes. However, this is not always true and it depends on how that binding is passed to the view. When the view is created this way, everything works as expected and InsideView is not updated when value hasn’t changed. @State private var value: Int = 0 InsideView(value: $value) In the example below, InsideView will be incorrectly updated even when value has not changed. It will be updated whenever its container is updated too. var customBinding: Binding<Int> { Binding<Int> { 100 } set: { _ in } } InsideView(value: customBinding) Can anyone explain this and say whether it's expected? Is there any way to avoid this behaviour that can ultimately lead to performance issues? Here's a sample project if anyone wants to play with it: import SwiftUI struct ContentView: View { @State private var tab = 0 @State private var count = 0 @State private var someValue: Int = 100 var customBinding: Binding<Int> { Binding<Int> { 100 } set: { _ in } } var body: some View { VStack { Picker("Tab", selection: $tab) { Text("@Binding from @State").tag(0) Text("Custom @Binding").tag(1) } .pickerStyle(SegmentedPickerStyle()) VStack(spacing: 10) { if tab == 0 { Text("When you tap a button, a view below should not be updated. That's a desired behaviour.") InsideView(value: $someValue) } else if tab == 1 { Text("When you tap a button, a view below will be updated (its background color will be set to random value to indicate this). This is unexpected because the view State has not changed.") InsideView(value: customBinding) } } .frame(width: 250, height: 150) Button("Tap! Count: \(count)") { count += 1 } } .frame(width: 300, height: 350) .padding() } } struct InsideView: View { @Binding var value: Int var body: some View { print("[⚠️] InsideView body.") return VStack { Text("I'm a child view. My body should be called only once.") .multilineTextAlignment(.center) Text("Value: \(value)") } .background(Color.random) } } extension ShapeStyle where Self == Color { static var random: Color { Color( red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1) ) } }
Replies
2
Boosts
0
Views
2.1k
Activity
Dec ’21
Swift in XCode
Hi there, I am trying to upload my AR experience from Reality Composer into XCode using Swift. Although I have followed the documentation from Apple's presentation, minute 40:53, (https://developer.apple.com/videos/play/wwdc2019/609/?time=2453) of working with AR and XCode, I receive an error message every time I try to load my scene, as the load prompt seems to become part of the scene title and my title itself is not recognized. For example: Module '...' has no member named 'load...'. If anyone can help me here, I would be very grateful.
Replies
0
Boosts
0
Views
472
Activity
Oct ’21
use ios13 to run my app have an Error : EXC_BAD_ACCESS
The ios14 to run my app, it is no problem. when run my app on the ios13.7 . The Xcode's console print an ERROR : EXC_BAD_SCCESS I look for the web for this question,but i can't find answer for this question . if you know answer ,write down your answer,please. Thank you!
Replies
0
Boosts
0
Views
406
Activity
Aug ’21
SwiftUI: conditional rendering according to the current device (iPhone or iPad)
Hello forum! I have a grid in my app I want to display 1 column if the user's device is iPhone or 2 columns if the device is an iPad How can I do that from SwiftUI? Could you help me? thankS
Replies
0
Boosts
0
Views
556
Activity
Aug ’21
How to set UIScreen.main.brightness in SwiftUI
I have a current app in the store written in Objective C that works OK, but I want to update it via SwiftUI as a universal app. In my original app, I adjust the display brightness for night time use by setting UIScreen.main.brightness = 0.2 for example. When exiting the application, I return it to its previous value. I have been unable to find out how this can be done in SwiftUI. Anyone know?
Replies
0
Boosts
0
Views
928
Activity
Aug ’21
Calling a local ViewController function from a SwiftUI View
How to correct call a view controller from content view? According https://www.hackingwithswift.com/books/ios-swiftui/wrapping-a-uiviewcontroller-in-a-swiftui-view created it. But still incorrect. Could you help me ? Thx.
Replies
1
Boosts
0
Views
2.4k
Activity
Jun ’21