The way to achieve Master and Detail structure, as of beta 4, it should be by using .navigationViewStyle(.doubleColumn). Works perfectly on iOS / iPadOS / macOS but not in tvOS... it's a bug or I'm missing something?
import SwiftUI
struct ContentView: View {
var arra = ["Margherita","Marinara","Calzone"]
var body: some View {
NavigationView {
List(arra, id: \.self) { pizza in
NavigationLink(destination: SecondView(pizza: pizza)) {
Text(pizza)
}
}.navigationBarTitle("MASTER")
SecondView(pizza: arra[0])
}
.navigationViewStyle(.doubleColumn)
}
}
struct SecondView: View {
var pizza : String
var body: some View {
Text(pizza)
.navigationBarTitle("DETAIL")
}
}
(as of GM release) The official doc tells something like: double column style will be stacked on tvOS, like it's an iPhone in portrait mode. So it's impossible to automatically achieve the "master and detail" look like it's an iPad in landscape, you have to build it for yourself. Even if you use one UISplitViewController in UIKit! With this behavior I think it's good to go only if the master it's a fullscreen CollectionView, sorta like Netflix App.
Related
I would like to redact a text on my widget when the device is locked. The below code works when I test it on iOS15, but didn't work on iOS16.
struct WidgetView : View {
var body: some View {
Text("Some sensitive text")
.privacySensitive()
}
}
Above applies to testing on both simulator and actual devices. I use xcode 14.2
I want to control low memory state in my SwiftUI app.
What is the alternative of applicationDidReceiveMemoryWarning in UIKit for SwiftUI when you choose SwiftUI lifecycle?
I found didReceiveMemoryWarningNotification for Notification Center, but it is also implemented on UIKit, not on Foundation.
Or if there is no alternative, should I use UIKit lifecycle?
Thanks
You can use a Combine publisher which detects UIApplication.didReceiveMemoryWarningNotification notifications.
Example:
struct ContentView: View {
private let memoryWarningPublisher = NotificationCenter.default.publisher(for: UIApplication.didReceiveMemoryWarningNotification)
var body: some View {
Text("Hello world!")
.onReceive(memoryWarningPublisher) { _ in
print("memory warning")
}
}
}
Result:
I've had my app inside a NavigationView using the StackNavigationViewStyle style for some time with no problems. Recently I wanted to add a sidebar to it though, so I thought I should try using the DoubleColumnNavigationViewStyle style for this. At the moment I can kind of make it work but it has some quirks:
If I am in a subview, and I try to slide back into its parent view, sliding back always brings the side bar into view instead of taking me back into the parent view which is what I would expect. Now matter how deep into my view hierarchy I am. (If you use the default Notes app and select View as a Gallery, that is exactly the way I expect my app to work like).
Much less important but annoying nonetheless is that if I press the back button, the nice animations of the sliding < back buttons into/out of view I got when I used StackNavigationViewStyle no longer happen. The buttons work fine but the animations are much worse now.
Here is a sample minimum app and a video to show what I mean:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
Text("Sidebar")
MainView()
}
}
}
struct MainView: View {
var body: some View {
Text("Main View")
NavigationLink(destination: Text("Sub View")) {
Text("Go to Sub View")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Thanks!
I have problem with making sidebar look transparent. All advices about implementing it (even official Apple Documentation) is on AppDelegate/SceneDelegate. But since last update projects have only this file:
'''
import SwiftUI
import UIKit
#main
struct SpenTApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
SideBarView()
.background(Color.clear)
DetailView()
}
.navigationViewStyle(DoubleColumnNavigationViewStyle())
}
}
}
Catalyst apps have some weird limitations, so to do what you want: SideBarView must be a List with listStyle(SidebarListStyle()).
How to create such a view floating view with a custom boarder as shown in the picture? And such that it disappears as soon as the user clicks outside of the view.
Normally you would do that with a Popover like this:
#State var isPresented = false
var body: some View {
Button(action: {
self.isPresented = true
}) {
Text("Press me")
}.popover(isPresented: $isPresented, arrowEdge: .top) {
Text("Pop!") // You can put you own custom view here for the popover
}
}
Although it works as intended on the iPad (and I believe tvOS too, but I haven't tested it), it does not work properly with the current version of SwiftUI (as of 10/12/2019) on iPhones. Currently, the above code will just result in a somewhat glitchy modal on an iPhone, which I don't think is the intended function of it on iPhones. Apple's documentation for popover isn't very helpful right now, but here it is anyway.
For you information .popover is unabailable in tvOS.