Swiftui - Cannot present Form inside Sheet or FullScreenCover - swiftui

When using a Form on a view and presenting it with either .sheet or .fullScreenCover the view is never presented and instead memory is quickly gobbled until the app crashes. Removing the form and replacing it with anything else works just fine.
Inside top level view, the sheet or full screen uses:
.fullScreenCover(isPresented: $showProfile, content: {
NavigationView {
ProfileNumberView()
}
})
OR
.sheet(isPresented: $showProfile, content: {
NavigationView {
ProfileNumberView()
}
})
The view it's launching can be as simple as:
var body: some View {
Form {
Text("TEST")
}
}
Again doing this causes the app to simply gobble up memory until a crash and no view is presented. If I simply remove the Form tag, the the view will present as normal. I have also tried embedding the NavigationView inside the form view (above the Form tag) but to no avail.
Why can I not present a view using a Form in this manner?
Update: I have discovered the source of the issue is when the top level view contains a List. If there is no list the screens present just fine, but it seems we cannot present a form within a cover or sheet if the parent has a list in it. Still not clear why that would be the case but commenting out the list and the sheet or fullscreencover worked.

The turned out to be something that was not directly related to the code I posted above. It's worth posting the answer though as I am sure others may run into this. The problem was styling given to the list in order to paint a background with an image. Specifically:
UITableViewCell.appearance().backgroundColor = .clear
let imageView = UIImageView(image: UIImage(named: "gradientBackground"))
imageView.contentMode = .scaleToFill
UITableView.appearance().backgroundView = imageView
This styling of the list for some reason prevents you from presenting a sheet or full screen cover that has a form. Very unclear why and I need this background, so while this particular issue is resolved, the functionality required is not.

Related

Dismiss Presented view without any click event

I am working with an app in SwiftUI.I have presented one view using
.sheet(isPresented: $doIWantThisViewToShowUser, content: {
DraggedUsersMenu()
})
DraggedUsersMenu()
is view I wanted to present. Here I have few stacks from which I need to navigate to another contentView. Now navigation Works perfectly My 2nd screen is there,but its behind this presented View.
Is there a way to dismiss() this view automatically, once we navigate to another controller.?
Add this: #Environment(\.presentationMode) var presentationMode.
And when you want to dismiss use: presentationMode.wrappedValue.dismiss()

SwiftUI Searchable several views down from NavigationView?

I'm having problems related to .searchable and am wondering if it's related to my application's view hierarchy. The examples I can find for using Searchable say to use it on the NavigationView directly, and show it like so:
var body: some View {
NavigationView {
List {
...some list content here...
}
.navigationTitle("My List")
}
.searchable(text: $queryString)
}
My NavigationView is a couple of views up from the list where I want to use .searchable. My hierarchy:
HomeView (has the NavigationView) -> LocationsView -> LocationListView
I want to use .searchable in the LocationListView, and I can - but it's not optimal. The Search bar is always visible, and it causes the navigation bar in my LocationDetailView (the next view down the hierarchy) to "jump" down when the view loads, and crowd the rest of my view, with a gap at the top.
Link to image w/nav title gap
I theorize that this is because I'm using .searchable in the LocationListView, which is inside the NavigationView. If I comment out .searchable in the List view, the Detail view does not have this behavior.
I'm wondering if there is a good recommendation for how to deal with this. I know I can't use a second NavigationView in the ListView, because then it's nested inside the first navigation view and has all of the associated issues. (Although it does fix the .searchable implementation!) Is there another way to solve this?

Remove Outline on Right-clicked Rows in SwiftUI Mac App List

I'm building a macOS app with SwiftUI, and I'm trying to remove (or even cover up) the border added to a List item when I right-click it.
Here it is by default:
Now with a right-click and a contextMenu view modifier:
I figured this is an NSTableView quirk, so I tried the approaches in these three Stack Overflow posts:
Customize right click highlight on view-based NSTableView
NSTableView with menu, how to change the border color with right click?
Disabling the NSTableView row focus ring
NSTableView: blue outline on right-clicked rows
I couldn't get any of those to work, and that may be due to the fact that I can't subclass an NSTableView, but can only override its properties and methods with an extension. Here's what I have so far that successfully removes the default table background and such:
extension NSTableView{
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
//Remove default table styles
backgroundColor = NSColor.clear
enclosingScrollView!.drawsBackground = false
selectionHighlightStyle = .none
}
}
Is there any way to remove that right-click border in SwiftUI? I'm even open to covering it with other views, but I can't seem to draw SwiftUI views in that space around the table cell.
I found a workaround for this. I put my List in a ZStack and then set its opacity to zero. I then built out a fully custom version of the same list, but using LazyVStack:
//Message List
ZStack{
//Ghost list for keyboard control
List($model.messages, id: \.self, selection: $model.selectedMessages){ $message in
MessageItemView(message: $message)
}
.focusable()
.opacity(0)
//Custom UI for the above List
ScrollView{
ZStack{
LazyVStack(spacing: 5){
ForEach($model.messagesToday){ $message in
MessageItemView(message: $message)
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
Each list is bound to the same model, so if I click a message to select it in the custom UI, the same thing gets selected in the invisible List. All the keyboard shortcuts that come with table use in a List look like they are working on the custom version.
So how does this solve my original problem? You can right-click on the custom MessageItemView and the default ring around the cell is invisible, but the contextMenu still works (it's defined inside my MessageItemView).
This isn't as elegant as I'd like, but it's nice to have 100% control over the UI but still get all the keyboard controls that come for free with a List.

SwiftUI preview's subviews are unselectable when embedded in NavigationView

When I embed my view's body inside of a NavigationView I lose all ability to select subviews within the preview window, as well as the reverse ability of selecting code to trigger and display the blue border around the associated subview in the preview. It only shows a blue border around the entire NavigationView no matter what or where I click. Is this a bug? If not, what am I doing wrong?
Edit: Per the request of the comments below, here is a simple example:
struct FooView: View {
var body: some View {
NavigationView {
VStack {
Label("Try")
Label("Selecting")
Label("These")
}
}
}
}

SwiftUI: Navigation bar of destination view has no background and is not animated on scroll

I'm trying to make navigation view that leads to destination view with scroll view, where navigation title of destination view would animate towards inline display mode or at least scroll behind the nav bar itself.
Basically I'm trying to replicate behavior of standard Music app, specifically when you go from Library to Songs.
There you have source view (Library) with its own title that is animated into inline display mode on scroll. When you tap Songs you also get list with new title (Songs) that also animates into inline display mode on scroll.
So I have main NavigationView with NavigationBarTitle. I move to destinationView with its own NavigationBarTitle and some long list of content. On scroll, NavigationBarTitle of main Navigation view changes to inline display mode, but NavigationBar of destination view behaves very odd: it's basically an overlay with no background and no animation.
And if you remove NavigationBarTitle of destination view all together it only makes things worse. It seems like it adds another transparent NavigationBar with nothing in it.
Also tried to add background to the navigation bar, looked around documentation, but found no solution.
Not sure if I'm doing something very wrong or it's just beta bug of SwiftUI or Xcode.
import UIKit
struct ContentView: View {
var body: some View {
NavigationView{
List(0..<20) { item in
NavigationLink(destination: DetailedView()) {
Text("Next view")
}
}
.navigationBarTitle("Source View")
}
}
}
struct DetailedView: View {
var body: some View {
List(0...25) { number in
Text("This is \(number)'th row")
}
.navigationBarTitle(Text("Destination View"))
// comment out line above to see empty frame of navigation bar
}
}
This is not a full answer to your question, but a temporary workaround: add top or vertical padding to your list on the child view, depending on your preference. This is what I have been doing until there is a better solution.
This will at least make the content scroll under the navigation header with a proper background rendered behind the header. It doesn't have the nice animation to make the title smaller.
struct DetailedView: View {
var body: some View {
List(0...25) { number in
Text("This is \(number)'th row")
}
.padding(.top)
.navigationBarTitle(Text("Destination View"))
}
}
This is fixed in the iOS 13.1 public release (with the App Store release of Xcode 11).
I'm currently on beta 5, and I think this is an ongoing bug with SwiftUI.
I noticed the same issue while doing the SwiftUI Landmarks Tutorials, and you can easily reproduce the issue: https://imgur.com/a/aYgUUH0
For now, to avoid seeing all the content scroll under a transparent navBar, I've converted all of my Navbars to display as inline, since automatic and large experience the issue.
List {
// ...
}
.navigationBarTitle(Text("MyTitle"), displayMode: .inline)