Conversation

Fahim Farook

I tried to explain the issues I was having with the current SwiftUI navigation options yesterday, but I donā€™t think I did a good job šŸ™‚ So hereā€™s a second stab at it ā€¦

Basically, my understanding is that the only out-of-the-box way to show new views is to either use a NavigationLink or a sheet. I had a List where I wanted the user clicking on the list cell to take them to one view and the user clicking on a button within the list view to take them to another view. (See screenshot #1). This was fairly easy to do in UIKit.

But when I use NavigationLinks for both items, they automatically add these disclosure indicators and doubles them for two NavigationLinks (yes, I know you can hide them with some jugglery, but you shouldnā€™t need to do that). Worse still, you still get taken to only one view no matter where you click šŸ˜›

Fine, so two NavigationLinks in the same cell wonā€™t work. So letā€™s try making one of them (the button) show a sheet instead. That should work, right?

And this does indeed work. (See screenshot #2).

So all is well? Well kinda ā€¦ First, I shouldnā€™t need to show a sheet when I donā€™t want to. If I want to push a new view in, then I should be able to do so, right?

But leaving that quibble aside, letā€™s say I want my cell to have a distinctive look. Like in screenshot #3. But now I have the disclosure indicator outside my content cell. And that really doesnā€™t look good to me ā€¦

Oh, but you can put the NavigationLink and the cell content in a ZStack to control that, you say? Sure, but then things look like screenshot #4 šŸ˜› Sure, with some more tweaking I can probably get things working right but shouldnā€™t this be easier?

Also, do note that you can set the opacity of the NavigationLink to 0 and hide the disclosure indicator altogether and yet have the NavigationLink work. And thatā€™s what I did in my app, but in that app, setting things up this way wouldnā€™t work for the two separate views. Tapping on the button would launch both the detail view and the other view.

But it works correctly here. So I think I must have gotten something subtle wrong in the other implementation.

However, if you want to avoid all these hassles, you might want to try out this package:

https://github.com/DoubleSymmetry/XNavigation

It let me do view navigation the way I wanted to, instead of having to use sheets when I didnā€™t want to use sheets šŸ˜›

If you have a better way to do what I was trying to do, please do let me know. Iā€™m always eager to learn šŸ™‚

Disclaimer: I am not affiliated to the developer of the package in any way. I just liked how it made navigation so much easier ā€¦ at least for the use cases I had.

#iOS #SwiftUI #Coding #Navigation #Apple
Screenshot #1. Trying to use twā€¦
Screenshot #2. The button/imageā€¦
Screenshot #3. Adding a backgroā€¦
Screenshot #4. The disclosure iā€¦
1
0
2

@f I was stumped in a similar way when I started learning SwiftUI. But there is a well-supported solution to this within Navigation Stack/SplitView: using ā€œprogrammatic navigationā€, which can be accomplished by using ā€œnavigation pathā€.

This WWDC talk goes over it, and generally the recommended navigation patterns: https://developer.apple.com/wwdc22/10054

I still feel that the navigation APIs are under developed in though. Hoping for some major capability enhancements in this .

1
0
0
@sids Thank you šŸ™‚ Will take a look ā€¦

Given how rich the navigation patterns are in UIKit, I do hope that we get some love this year for navigation. Letā€™s wait and hope I guess?
0
0
0