I'm trying to make my iPhone apps (targeting iOS 15 and above) fully compatible with iOS 16 without success!
I don't know how to have NavigationView for iOS 15 and NavigationStack for iOS 16 in the same piece of code.
The code above isn't accepted by Xcode:
if #available(iOS 16, *) {
NavigationStack {
} else {
NavigationView {
}
From: SwiftUI NavigationView/Stack if available iOS 15/16
I guess that creating a custom container, as suggested by #timmy, will be the only solution. But I don't know how to proceed.
What's the solution then?
Personally I wouldn't use NavigationStack unless I would target iOS 16+ but if you want to do that you could make your own Navigation Wrapper
struct MyNavigation<Content>: View where Content: View {
#ViewBuilder var content: () -> Content
var body: some View {
if #available(iOS 16, *) {
NavigationStack(root: content)
} else {
NavigationView(content: content)
}
}
}
and then just use it, like you would NavigationStack or NavigationView and on iOS 15 or older it would use NavigationView and on iOS 16 or newer it would use NavigationStack
Your code isn't accepted by Xcode because it isn't valid. You cannot have a { without a } in the same block.
I have found that using NavigationView can present problems on both iPhone and iPad apps running under iOS 16, even though NavigationView is only deprecated for now. On an iPhone, views reached from a NavigationLink often close themselves as soon as they are opened. On an iPad, the same problem occurs and the generation of Back arrows appears to be a bit random, especially in document apps. I have found it well worth making the effort to use NavigationSplitView and NavigationStack, even though this has involved me writing quite a lot of extra code to achieve pleasing results, particularly in apps designed to run at their best on both iPhone and iPad. That said, Apple do provide some clear advice on how to adopt the new Views here.
I have come across another oddity with iOS 16. Pickers in modal sheets, which have their list arrays populated .onAppear, no longer work as intended and the Picker selection can no longer be set programmatically. You have to populate the Picker's list before activating the modal sheet and pass it to the Sheet as a Binding.
Thanks halo for a top tip on how to use if #available().
Related
In my Mac app, I want to display a local PDF file through a new view when I tap a button. The StackOverflow post (here) shows how to do it for an iOS app using UIViewRepresentable, but this does not work for a Mac app.
macOS 11, iOS 14
There is a native SwiftUI view modifier for the QuickLook preview since macOS 11 / iOS 14. You can set the binding to the URL to your PDF File (or any other type that can be shown in QuickLook). Then the system presents the file. If the binding value is nil, the preview will not show or will be dismissed if currently shown.
.quickLookPreview(_ item: Binding<URL?>)
This answer is just to add more information to mw_906's answer above. I created a file called UserGuide.pdf and placed it into my app's bundle. The syntax of my ContentView file is
import SwiftUI
import QuickLook
struct ContentView: View {
#State var userGuideUrl: URL?
Button(action: {
userGuideUrl = Bundle.main.url(forResource: "UserGuide", withExtension: "pdf")
} ) {
Text("UserGuide")
}
.help("This button displays the User Guide.")
.quickLookPreview($userGuideUrl)
}
}
Now that Apple has provided such a simple way of displaying files, I encourage all Apple developers to use it to include User Guides and other helpful documentation for their apps.
In Xcode 12, many XIBs are stuck with "View as: iPhone 11"—how can I fix this? I need to view as iPad.
Here is an example:
Similar questions about this (from past Xcode versions) have said to change "Size" property in "Simulated Metrics" in the Attributes inspector. However, here is what the options are in that list:
Bizarrely, most of my XIBs render as iPad but I can't change which iPad model is used. My app target only has iPad and Landscape orientations enabled:
So I'm very confused why it would show anything other than landscape iPad options in Interface builder. Also, my scheme is set to build on iPad.
Please let me know a solution!
Look at your first screen shot in the question. Look at the lower left. See where it says "View as iPhone 11"?
That's a menu. Click it and choose a different device from the menu.
I have been looking for an answer for an hour. But so far I only find solutions with a NaviagtionView. I do not want to do that. I have a single simple window. Similar to Safari or the system settings.
I am not able to add a toolbar to this window.
What I tried so far:
In AppDelegate is set
window.toolbarStyle = .unified
But it seems to have no effect on the window.
Then I added a toolbar to my ContentView
.toolbar {
ToolbarItem{
Label("Hello", systemImage: "book.circle")
}
Still I see nothing of it.
Update:
It works fine with a new Projekt based on a »SwiftUI App« as Life Cycle. If I choose App Delegate I get no toolbar. Seems to me there is something in the App Delegate I have to set.
If I start my app on any iPad, my app does not run. It just shows a white screen and nothing happens.
The app runs perfectly fine on an iPhone simulator.
I tried closing/opening Xcode and erasing all simulators, but nothing seem to work.
Im on Xcode 11 stable release
If it is a SwiftUI application, then by default, the navigation style is set to Double Columns.
It consists of a Master View and a Detail View, side by side.
If you try running it in simulator in landscape mode, you will see your view on the left
I will assume that you only have the Master View.
So add the modifiier .navigationViewStyle(StackNavigationViewStyle()) AFTER the closing brace of Navigation View.
NavigationView {
...
}
.navigationViewStyle(StackNavigationViewStyle())
so, i have an ios app and i have built it with interface builder and xcode. I built it using interface builder in the horizontal view because that is how i want my app. I have also made the supported views landscape left and right and i have edited info.plist to make it open horizontally.
But when i build and go it opens in iphone simulator horizontally but it shows as it would if it was vertical.
here's some screenshots:
what i want it to look like:
http://www.sendspace.com/file/4ct4b0
what it actually looks like:
http://www.sendspace.com/file/fayjqj
how could i get it to open like i want??
You have to allow the orientation also in your implementation of UIViewController using shouldAutorotateToInterfaceOrientation :
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return YES; //or UIInterfaceOrientationIsLandscape(interfaceOrientation) for just landscape
}
Also, if you have some more problems look on linked SO questions to this "out-dated" question:
iPhone app in landscape mode, 2008 systems