Xcode 12: environment overrides setting in panel is not reflected on Simulator - environment

I follow a YouTube video on new stuff of iOS 14. Now we could set environment overrides via the panel. But I found the changes like Text Dynamic type is not reflected on iOS simulator. I search around, it is quite new, no related information I found. Is there anyone met the same issue here, how to fix it?
There is a warning "2020-07-18 11:46:25.479202+0800 SwiftUITest[32945:888273] libMobileGestalt MobileGestaltCache.c:38: No persisted cache on this platform." in the console. Not sure if it is related.
MacOS: 10.15.6 Beta (19G71a)
Xcode: 12.0 beta 2 (12A6163b)
import SwiftUI
struct ContentView: View {
#State private var text = "Hello, world!"
var body: some View {
TextEditor(text: $text)
.padding()
.foregroundColor(.blue)
.font(.custom("Georgia", size: 24, relativeTo: .largeTitle))
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Related

how to use NavigationLink(value:label:) inside a List within a NavigationStack or NavigationSplitView

.navigationTitle("Login")
.padding()
.frame(maxHeight: .infinity,alignment: .top)
.background{
NavigationLink(tag: "VERIFICATION", selection: $otpModel.navigationTag) {
Verification()
.environmentObject(otpModel)
} label: {}
.labelsHidden()
}
.alert(otpModel.errorMsg, isPresented: $otpModel.showAlert) {}
}
}
struct Login_Previews: PreviewProvider {
static var previews: some View {
Login()
}
}
}
error message- 'init(tag:selection:destination:label:)' was deprecated in iOS 16.0: use NavigationLink(value:label:) inside a List within a NavigationStack or NavigationSplitView
we try to set up code for login in iOS but following message appears and hence code was nearly completed but due to deprecated issue not able to resolve
error message- 'init(tag:selection:destination:label:)' was deprecated in iOS 16.0: use NavigationLink(value:label:) inside a List within a NavigationStack or NavigationSplitView

How can I preview a SwiftUI View shared between iOS and WatchOS?

I have a sample SwiftUI View I am using in a iOS with Apple Watch Extension and I am getting this error when I try to preview it in a WatchOS "Device":
The run destination iPhone 12 mini is not valid for Running the scheme 'TestApp WatchKit App'
I have tried everything. Restarting Xcode like suggested here was one of the first things.
The way I got it to "work" eventually was with a pinned preview.
This is my sample View, it lives in a "SharedViews" Folder and it has Target Membership in TestApp as well as TestApp WatchKit Extension:
struct CustomTextView: View {
let caption: String
let foreground: Color
var body: some View {
VStack {
Text(caption)
.padding()
Text("Colored\r" + caption)
.foregroundColor(foreground)
.padding()
}
}
}
//struct CustomTextView_Previews: PreviewProvider {
// static var previews: some View {
// CustomTextView(caption: "Shared Preview", foreground: .blue)
// }
//}
To preview this View in a Watch Device I created a "Helpers" Folder in TestApp WatchKit Extension and inside this I created a "Container View" which is only Member of TestApp WatchKit Extension:
struct CustomTextViewWatchPreview: View {
var body: some View {
CustomTextView(caption: "Watch Preview Container", foreground: .red)
}
}
struct CustomTextViewWatchPreview_Previews: PreviewProvider {
static var previews: some View {
CustomTextViewWatchPreview()
}
}
Now to preview my CustomTextView I open the ..Preview File and pin it's preview. Then I switch back to the CustomTextView file, apply my changes and see them in the pinned preview. To get rid of the "cannot preview..." error I comment-out the own preview of the CustomTextView like above.
To preview in iPhone I change the run target, uncomment the own preview and unpin the Watch Preview.
Does anyone have a better solution? Maybe one where I can preview my View in iPhone and Apple Watch at the same time?

SwiftUI: How to preview a view on a physical provisioned device?

When using SwiftUI view previews, is there a way to get it displayed on my physical device?
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
}
}
Yes, simply activate "Preview on device" in the canvas view:

SwiftUI NavigationLink Text click not working [duplicate]

I was working on an application with login and after login there are categories listed. And under each category there are some items listed horizontally. The thing is after login, main page appears and everything is listed great. When you click on an item it goes to detailed screen but when you try to go back it just crashes. I found this flow Why does my SwiftUI app crash when navigating backwards after placing a `NavigationLink` inside of a `navigationBarItems` in a `NavigationView`? but i could not solve my problem. Since my project become complicated, I just wanted to practice navigation in swiftui and I created a new project. By the way I downloaded the latest xcode version 11.3. I wrote a simple code as follows:
NavigationView{
NavigationLink(destination: Test()) {
Text("Show Detail View")
}
.navigationBarTitle("title1")
And Test() view is as follows:
import SwiftUI
struct Test: View {
var body: some View {
Text("Hello, World!")
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Test()
}
}
As you can see it is really simple. I also tried similar examples on the internet but it does not work the way it suppose to work. When I run the project, I click the navigation link and it navigates to Test() view. Then I click back button and it navigates to the main page. However, when I click the navigation link second time, nothing happens. Navigation link works only once and after that nothing happens. It does not navigate, it des not throw any error. I am new to swiftui and everything is great but the navigation. I tried many examples and suggested solutions on the internet, but nothing seems to fix my issues.
[UPDATE] Nov 5, 2020 - pawello2222 says that this issue has been fixed in Xcode 12.1.
[UPDATE] Jun 14, 2020 - Quang Hà says that this issue has come back in Xcode 11.5.
[UPDATE] Feb 12, 2020 - I checked for this issue in Xcode 11.4 beta and found that this issue has been resolved.
I was getting the same issue in my project too, when I was testing it in Xcode's simulator. However, when I launched the app on a real device (iPhone X with iOS 13.3), NavigationLink was working totally fine. So, it really does seem like Xcode's bug.
Simulator 11.4: This issue has been fixed
You need to reset the default isActive value in the second view.
It works on devices and emulators.
struct NavigationViewDemo: View {
#State var isActive = false
var body: some View {
NavigationView {
VStack {
Text("View1")
NavigationLink(
destination: NavigationViewDemo_View2(isActive: $isActive),
isActive: $isActive,
label: { Button(action: { self.isActive = true }, label: { Text("click") }) })
}
}
}
}
struct NavigationViewDemo_View2: View {
#Binding var isActive: Bool
var body: some View {
Text("View2")
.navigationBarItems(leading: Button(action: { self.isActive = false }, label: { Text("Back") }))
}
}
Presumably this will be resolved when Apple fixes the related bug that prevents 13.3 from being selectable as a deployment target.
I'm experiencing the same issue as everyone else. This issue is present in simulator and preview running 13.2, but is fixed when deploying to my own device running 13.3.
As #Александр Грабовский said its seems like a Xcode 11.3 bug, I am encountering the same problem, you must downgrade or use some workaround like custom back button as below
struct ContentView: View {
#State private var pushed: Bool = false
var body: some View {
NavigationView {
VStack {
Button("Show Detail View") {
self.pushed.toggle()
}
NavigationLink(destination: Test(pushed: $pushed), isActive: $pushed) { EmptyView() }
}.navigationBarTitle("title1")
}
}
}
struct Test: View {
#Binding var pushed: Bool
var body: some View {
Text("Hello, World!")
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: BackButton(label: "Back") {
self.pushed = false
})
}
}
struct BackButton: View {
let label: String
let closure: () -> ()
var body: some View {
Button(action: { self.closure() }) {
HStack {
Image(systemName: "chevron.left")
Text(label)
}
}
}
}
For anyone who's having the same symptom with other versions of iOS than the buggy beta identified by other answers, there's another reason you might be seeing this behaviour.
If your NavigationLink is nested inside another NavigationLink, the inner NavigationLink will only work once, unless you add isDetailLink(false) to the outer link.

NavigationLink Works Only for Once

I was working on an application with login and after login there are categories listed. And under each category there are some items listed horizontally. The thing is after login, main page appears and everything is listed great. When you click on an item it goes to detailed screen but when you try to go back it just crashes. I found this flow Why does my SwiftUI app crash when navigating backwards after placing a `NavigationLink` inside of a `navigationBarItems` in a `NavigationView`? but i could not solve my problem. Since my project become complicated, I just wanted to practice navigation in swiftui and I created a new project. By the way I downloaded the latest xcode version 11.3. I wrote a simple code as follows:
NavigationView{
NavigationLink(destination: Test()) {
Text("Show Detail View")
}
.navigationBarTitle("title1")
And Test() view is as follows:
import SwiftUI
struct Test: View {
var body: some View {
Text("Hello, World!")
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Test()
}
}
As you can see it is really simple. I also tried similar examples on the internet but it does not work the way it suppose to work. When I run the project, I click the navigation link and it navigates to Test() view. Then I click back button and it navigates to the main page. However, when I click the navigation link second time, nothing happens. Navigation link works only once and after that nothing happens. It does not navigate, it des not throw any error. I am new to swiftui and everything is great but the navigation. I tried many examples and suggested solutions on the internet, but nothing seems to fix my issues.
[UPDATE] Nov 5, 2020 - pawello2222 says that this issue has been fixed in Xcode 12.1.
[UPDATE] Jun 14, 2020 - Quang Hà says that this issue has come back in Xcode 11.5.
[UPDATE] Feb 12, 2020 - I checked for this issue in Xcode 11.4 beta and found that this issue has been resolved.
I was getting the same issue in my project too, when I was testing it in Xcode's simulator. However, when I launched the app on a real device (iPhone X with iOS 13.3), NavigationLink was working totally fine. So, it really does seem like Xcode's bug.
Simulator 11.4: This issue has been fixed
You need to reset the default isActive value in the second view.
It works on devices and emulators.
struct NavigationViewDemo: View {
#State var isActive = false
var body: some View {
NavigationView {
VStack {
Text("View1")
NavigationLink(
destination: NavigationViewDemo_View2(isActive: $isActive),
isActive: $isActive,
label: { Button(action: { self.isActive = true }, label: { Text("click") }) })
}
}
}
}
struct NavigationViewDemo_View2: View {
#Binding var isActive: Bool
var body: some View {
Text("View2")
.navigationBarItems(leading: Button(action: { self.isActive = false }, label: { Text("Back") }))
}
}
Presumably this will be resolved when Apple fixes the related bug that prevents 13.3 from being selectable as a deployment target.
I'm experiencing the same issue as everyone else. This issue is present in simulator and preview running 13.2, but is fixed when deploying to my own device running 13.3.
As #Александр Грабовский said its seems like a Xcode 11.3 bug, I am encountering the same problem, you must downgrade or use some workaround like custom back button as below
struct ContentView: View {
#State private var pushed: Bool = false
var body: some View {
NavigationView {
VStack {
Button("Show Detail View") {
self.pushed.toggle()
}
NavigationLink(destination: Test(pushed: $pushed), isActive: $pushed) { EmptyView() }
}.navigationBarTitle("title1")
}
}
}
struct Test: View {
#Binding var pushed: Bool
var body: some View {
Text("Hello, World!")
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: BackButton(label: "Back") {
self.pushed = false
})
}
}
struct BackButton: View {
let label: String
let closure: () -> ()
var body: some View {
Button(action: { self.closure() }) {
HStack {
Image(systemName: "chevron.left")
Text(label)
}
}
}
}
For anyone who's having the same symptom with other versions of iOS than the buggy beta identified by other answers, there's another reason you might be seeing this behaviour.
If your NavigationLink is nested inside another NavigationLink, the inner NavigationLink will only work once, unless you add isDetailLink(false) to the outer link.

Resources