:-) Here comes a probably silly problem with a bit of a preamble sorry!
Long story short - I've been developing an app pulling my hair out due to a bug with the TabView in SwiftUI. I was overlaying the tabs with a ZLayer to add a funky big button in the middle - so when I was running into problems I thought this would be the issue... but this becomes irrelevant in a moment.
For simplicity I started a New project in XCode v11.6, build target iOS 13.6, Selected Tabbed App, created a very simple boilerplate SwiftUI content view with 2 tabs. (For clarity - I did not modify the default code at all). Runs perfectly on the simulator. Plug in my iPhone XS running iOS 13.6.1 (when I first encountered this problem I was on 13.5 so have updated iOS builds whilst this problem has occurred). Run the simple, non modified app on my phone. Loads up "First View", I tap the "Second" tab, nothing. No response at all. This is the same problem I am having in my more complicated app. Occasionally if I keep rebuilding. It will let me switch to "Second View" but then cannot switch back. The Tab Controller just seems to become non-responsive.
Now back to my more complicated app, on the first view there is a scroll view and there is a button with an attached actionSheet. If I pop open the action sheet, then dismiss it (regardless of just calling the .cancel() button or selecting an action), the Tab Controller works perfectly. There is another option which opens another sheet, again as soon as something has been overlayed, everything works as expected until the next app launch.
Tried resetting my MacBook, tried resetting my iPhone, tried uninstalling the build then rebuilding, cleaning the build folder, tried creating multiple projects with build targets iOS 13, 13.2, 13.5, 13.6. Problem seems to persist, but every time the simulator works perfectly.
So my questions here are: From my searching I can't find anyone with this problem. Is it just me really? Can anyone with XCode 11.6 and an iPhone XS please simply hit "New Project" -> "Tabbed App" and let me know if this works? (I'm almost wondering if there's a bug in XCode 11.6 when you create an app and everyone is either working on projects that were already created pre XCode 11.6 or already using the beta XCode... But really I am just out of ideas)
Also is there any deeper debugging one can do? Something like actually notifying me for every touch it receives and whether the App sandbox is getting the touch or the OS is (for some reason) keeping the tap gesture? I've never experienced something like this so I can't really see where to start debugging as I don't even know if the app is receiving the tap.
Or frankly - does anyone have any other ideas?
Just for clarity - ContentView looks like this: (Again it's straight from what XCode creates for a Tabbed App)
import SwiftUI
struct ContentView: View {
#State private var selection = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
VStack {
Image("first")
Text("First")
}
}
.tag(0)
Text("Second View")
.font(.title)
.tabItem {
VStack {
Image("second")
Text("Second")
}
}
.tag(1)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Thanks in advance and sorry for the long ramble - from a very sleepy dev at wits' end!
Related
Every time I am trying to resume my preview canvas in Xcode I get this annoying error. I tried restarting, moving the project to another location, and changing the preview device. The current project is a fresh one, just started building a view.
Strange thing is that when I run it on the iOS Simulator it works. The app builds successfully. I also noticed that this is only happening when I use source control with my project(GitHub). Not using it is not an option for me.
Let me know if I need to add the whole crash report, because it's very long.
Here is the full crash log
https://developer.apple.com/forums/content/attachment/5f8f5c96-7c1e-4eef-b0d7-ed59894a9c30
You didn't add your code so I can't help specifically but here are some of the reasons it happened to me in the past:
If you use an environment object on the view you're previewing and you don't add it to preview, it causes a crash:
struct MyView_Previews: PreviewProvider {
static var previews: some View {
MyView()
.environmentObject(ViewModel())
}
}
Sometimes I just add ZStack to it and it magically solves the problem:
struct MyView_Previews: PreviewProvider {
static var previews: some View {
ZStack {
MyView()
}
}
}
Sometimes It helps to clean the project (command + shift + K) and build again
Basically if your project works in simulator it means there's nothing wrong with it, it builds. The only problem is that you can't use the preview. You probably didn't set it right, it's missing something it needs in order to run properly. Try to figure it out. Or just run on simulator like we did before there was such a thing as previews..
I want my print message to be shown in the Debug Area in Xcode 13. There are several questions like this on StackOverflow, some of them suggest using the Debug Mode, which seems to have been removed in Xcode 13.
Here is my ContentView:
struct ContentView: View {
var body: some View {
Button("button", action: {
print("abc")
})
}
}
Though I use print("abc"), when I click the button, there's nothing in the Debug Area. How to show it?
The problem is that you're not running the app. You're just looking at the preview. Press Command-R and run the app in a simulator; tap the button in the simulator.
Note that when you're actually running the app, the debug bar (at the top of the debug area) looks quite different:
I have a simple use case where a screen pushes another screen using the NavigationLink. There is a strange behaviour iOS 14.5 where the pushed screen is popped just after being pushed.
Code:
NavigationLink(destination: EmptyView()) { EmptyView()}
I manage to create a sample app where I reproduce it. I believe the cause is the presence of #Environment(\.presentationMode) that seem to re-create the view and it causes the pushed view to be popped.
The exact same code works fine in Xcode 12 / iOS 14.4
I was stuck in this since last week. To fix, I simply added this to my view containing my existing NavigationLinks:
NavigationLink(destination: EmptyView()) {
EmptyView()
}
I am new to Xcode, macOS development etc.. So maybe its just because I am new - but I could not make a simple printout to console work with all effort.
I created a minimum nonworking example of my problem:
import SwiftUI
struct Test: View {
#State var message = "Test"
var body: some View {
Button(action: {
print("test worked")
message = "test worked"
}) {
Text(message)
}
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Test()
}
}
When executing this the Button text changes, but nothing appears in console.
I tried by following this tutorial, but it did not work with it either: https://www.hackingwithswift.com/read/18/2/basic-swift-debugging-using-print
I tried by enabling debug preview according to this thread: How to print() to Xcode console in SwiftUI?
And I tried by enabling this setting:
Nothing helps.. I am using Xcode Version 12.1 (12A7403) btw.
You cannot print to the console from a SwiftUI preview.
The only possibility for outputting debug info in a preview is to display your logs in a Text (or any other UI element) that's displayed in your Preview.
However, if you need proper debugging, run the full app, don't use previews. Previews are great for initial wireframes, but once you get to the stage where you need debugging, switch to using the view in your app and running that on the Simulator (or a real device) rather than using the preview.
I have faced the same problem nothing shows in console.This helps me. Make sure you mark right side button to see the debug console.
It's possible to debug SwiftUI previews and print in the console without launching the app on a device or simulator. From the canvas, make sure to click on "Debug preview". More info can be found on Apple website.
Xcode 12
Long press on the Live Preview button, then click on Debug Preview.
Xcode 11
Right-click (or Control-click) on the Live Preview button in the bottom right corner of the preview.
I'm also having the same problem. I think it's a bug. I am pretty sure it was working on some previous xcode versions.
Anyway as a workaround you can just set a breakpoint on the line where you 'd like to log something and manually set a debugger command action like you can see in the picture.
Don't forget to tick the checkbox if you want to just print the log without actually stopping.
I was building an application at the beginning of this year up until around March, and then I took a break and recently wanted to pick it up again. However, my code was behaving differently from when I last used it. I had not made any changes to the code, and strangely even past versions from months prior also weren't working, and those certainly weren't changed.
From some troubleshooting here are things that worked based on this code:
Here is the main ContentView class
var body: some View {
return ZStack {
setColor
.edgesIgnoringSafeArea(.all)
.statusBar(hidden: true)
VStack(alignment:.trailing){
VStack{
DisplayBar(majorKeyManager:majorKeyManager)
}.zIndex(2.0)
VStack{
GuitarView(guitarModel:guitarModel)//.position(x:556, y:220)
}.padding(.bottom, 20)
VStack{
PianoView(pianoModel: pianoModel)//majorKeyManager: majorKeyManager, audioEngine: audioEngine)//.position(x:556,y:600)
}
}
}.edgesIgnoringSafeArea(.top)//.padding(10)
}
Then here is the GuitarView
struct GuitarView: View {
#ObservedObject var guitarModel:GuitarModel
var body: some View {
var needsMarker = false
var needsIndexMarker = false
var needsCapo = false
return HStack (spacing:0){
VStack{
ForEach(0..<guitarModel.numStrings){ stringIndex in
VStack{
Here is the PianoView
struct PianoView: View {
#ObservedObject var pianoModel:PianoModel
var body: some View {
return VStack(alignment:.leading){
ZStack{
HStack (spacing:2){ // DRAW WHITE KEYS
ForEach(0..<self.pianoModel.whiteKeyMap.count){ (keyIndex) -> KeyButtonView in
I didn't think it was important to show the details of what is happening within GuitarView/PianoView. They are structured very similarly, except the difference is the PianoView always behaves properly, while the GuitarView is hit or miss. I placed a red line in the GuitarView image that shows where it is not entering the ForEach, yet it is meeting the conditions of the ForEach (the variable it is checking is always 12).
Both views are being updated based on their respective models which are ObservedObjects. The model is correctly being updated for both the Guitar and Piano. However, GuitarView gets called once when the initial program is launched, but is sometimes never called again. However, this varies depending on whether I'm running it within device simulator, versus a real device and which XCode version I am using.
On XCode 11.4.1, the code does not work properly on the device simulator on any device (Mac, iPhone, iPad, etc.). However, it does run properly on real devices like an iPhone and an iPad. I tried reinstalling XCode but that changed nothing.
Then, I tried rolling back my version of XCode to 11.2.1, and the code runs properly in the iPad simulator, but not as a Mac simulator.
Has anyone had any similar problems to this? I can't seem to figure out why this code gets called once, but then never gets called again despite its ObservedObject being clearly updated, and another nearly identical view (PianoView), is clearly working.