Big Sur-style sidebar Mac Catalyst in SwiftUI 2 - swiftui

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()).

Related

Strokes disappearing in SwiftUI PencilKit

I was really comfortable using PencilKit in SwiftUI, however I revisited a project and apparently there is a bug in Xcodes new version where strokes disappear after drawing them in the simulator.
I am running Version 14.1 of Xcode.
I set up a minimal code example to show my problem:
import SwiftUI
import PencilKit
struct ContentView: View {
var body: some View {
PKCanvasRepresentation()
}
}
struct PKCanvasRepresentation : UIViewRepresentable {
func makeUIView(context: Context) -> PKCanvasView {
var canvas = PKCanvasView()
canvas.drawingPolicy = .anyInput
return canvas
}
func updateUIView(_ uiView: PKCanvasView, context: Context) {
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
This does not draw correctly in the preview or simulator but it works if I run it on a non-virtual testing device. Does anyone found a solution to this, as it makes debugging really uncomfortable.
this is an Xcode 14 bug that specifically happens with an iOS 16 simulator.
At first I suspected the hardware, but this was not confirmed. With the same project, hardware, Xcode and simulator, it is simply random whether it works.

What is the alternative of applicationDidReceiveMemoryWarning in UIKit for SwiftUI?

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:

Detecting hardware keyboard key press under iPadOS 14.x with SwiftUI 2.0

i'm trying to add hardware keyboard support for an iOS 14.0 app written in SwiftUI 2.0.
I saw some examples working with UIHostingController, so i'd like to try this way on iOS14/SWiftui 2.0 using WindowGroup.
I'm gettin gerror when compiling in XCODe 12.3
"Generic struct 'WindowGroup' requires that 'KeyTestController' conform to 'View'"
ContentView() conforms to View and everything is working fine when not using the "KeyTestController" class.
Any way to solve this ?
Thank you very much.
import SwiftUI
import StoreKit
import UIKit
#main
struct myApp: App
{
var body: some Scene
{
WindowGroup
{
KeyTestController(rootView:ContentView())
}
}
}
class KeyTestController<Content>: UIHostingController<Content> where Content: View
{
/* CODE …… */
}

How do I create a simple app with a side bar in SwiftUI?

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!

SwiftUI achieve Master and Detail structure in tvOS

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.