Swiftui [Bug] Navigationview and List Not Showing on iPad Simulator Only

Swiftui [BUG] NavigationView and List not showing on iPad simulator only

According to answer of @Procrastin8, I am here to show the example code, you just need to add one line of code .navigationViewStyle(StackNavigationViewStyle()

import SwiftUI

struct LandmarkList: View {
var body: some View {
NavigationView {
List(landmarkData) { landmark in
NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
.navigationBarTitle(Text("Landmarks"))
}.navigationViewStyle(StackNavigationViewStyle())
}
}

struct LandmarkList_Previews: PreviewProvider {
static var previews: some View {
ForEach(["iPhone SE", "iPhone XS Max"], id: \.self) { deviceName in
LandmarkList()
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}
}
}

Screenshot

Sample Image

Only tab view and button text loading in Simulator/iPhone, but app working in preview

Issue 1:
Your phone is in Dark mode while your Previews are in Light mode. It's hard to say what effect that is having, since you haven't included any code.

Issue 2:
Again, because you haven't shown any code, it's tough to say why the background isn't showing up. My suspicion is that it's an image and you have it in your Preview assets, but not being copied to your actual app bundle.

If it's a gradient that is defined in code, you may have colors defined for Light mode but not dark mode.

First thing I'd try is switching your phone to Light mode. Then, make sure that your background image (if there is one) is included in your app's assets.

Color Asset not showing on iPad Device in Swift UI

looks like there was the bug in Xcode , where it doesn't pick color assets from asset file , so had to create color extension file to pick color programmatically using RGB Color

Content of NavigationView not showing up SwiftUI

Instead of this

NavigationLink(destination: PlayersView()) {
Text("Players")
.foregroundColor(.white)
.padding(12)

}
.background(Color.black)
.cornerRadius(12)

do this

NavigationLink(destination: PlayersView()) {
Text("Players")
.foregroundColor(.white)
.padding(12)
.background(Color.black)
.cornerRadius(12)
}

Update: I assume I've got your issue, which I at first just passed automatically. NavigationView is platform specific. By default iPad has Master/Details navigation style, which has different presentation depending on orientation, so you don't see in Preview your link, because Preview is in Portrait and navigation link is in Sidebar, which is hidden.

If you want to see it in such Preview, you need to use .navigationViewStyle(StackNavigationViewStyle()) for NavigationView. Or, in your variant, just run application in Simulator and using CMD-Left/Right arrow rotate Simulator.

Note: It's not possible to rotate Preview or now. Just in case.

NavigationLink in SwiftUI not working anymore

I can see from the comments that you've found out it will only work in a NavigationView and are now wondering why. It only matters that your view is embedded in a NavigationView directly above it the View hierarchy. For example, this code would work:

struct FirstView: View {
var body: some View {
NavigationView {
NavigationLink(label: "Go to next view", destination: NextView())
}
}
}

struct NextView: View {
...

While this won't:

struct FirstView: View {
@State var modalPresented = false
var body: some View {
NavigationView {
Button("Show fullscreen cover"){
modalPresented = true
}
}
.fullScreenCover(isPresented: $modalPresented, content: SecondView())
}
}

struct SecondView: View {
var body: some View {
NavigationLink(label: "Go to next view", destination: NextView())
// Doesn't work because the view is in a fullScreenCover and therefore not a part of the NavigationView.
}
}

when I open the app on ipad simulator the design is broken

This is down to how NavigationView works on iPads (and also larger iPhones in landscape).

The first view given to NavigationView acts as the collapsible left hand navigation, which is a fixed width. Any NavigationLink destinations in that view will open in the main, “detail” view that takes up the full screen.

You can specify a second view underneath the first one to provide a ‘default’ view to display in the main screen:

NavigationView {
// the sidebar view
ScrollView {
// etc.
}
// the default view
Text("Default view")
}

You could also add a third view, which will automatically give your iPad a three-column view similar to that used by Mail, etc. if you wanted to.

Another option is to force the NavigationView to work exactly the same way as it does for an iPhone in portrait mode, by adding a .navigationViewStyle argument:

NavigationView {
// contents as before
}
.navigationViewStyle(.stack)

While that will give you an iPhone-like experience on the iPad, it doesn’t really take full use of the larger screen space without careful design work. For that reason, it’s usually a good idea to invest some time in coming up with an app design that is tailored to the default iPad style of navigation view.



Related Topics



Leave a reply



Submit