Use NavigationLink programmatically in SwiftUI - apple-watch

I'm looking for a way to display a view in my WatchOS application "one level in" from the NavigationView on app startup if certain conditions are met.
I want the same effect as if I would have pressed a NavigationLink myself, i.e being able to go back to the NavigationView again, which is not possible if I choose to display the desired view right away.
"navigationBarItems" is also not available for WatchOS which makes it impossible to navigate backwards without actually clicking the NavigationLink in the first place.
Is there a way to programmatically "click" a NavigationLink in order to achieve this?

I don't have any experience with watchOS, but I've used the the "isActive" variant of NavigationLink for similar app needs.
In your initial view, define a #State var showOneLevelIn = false (or some other binding) and set that state var to true if you want the view to auto navigate to your second level. The label for the NavigationLink is an EmptyView() so it won't be visible in your initial view.
NavigationLink(destination: OneLevelInView(), isActive: $showOneLevelIn, label: { EmptyView() })

Related

NavigationLink make a lot of blank spaces

I'm currently working on a app ,and I encounter a weird problem , I use NavigationLink like this
NavigationLink(
destination: ContentView(),
label: {
Image(systemName: "house.fill")
})
This is the View before pressing home and back to the same view
I used this .navigationBarBackButtonHidden(true)and .navigationBarHidden(true) and
This is the View after a couple of back and forth
I'm curious if anyone encounter this problem and solve this .
It made a weird spacing on top
That didn't work , I delete the .navigationBarBackButtonHidden(true) so you can see what happen if I go to ContentView and back to the same View.
Going back and forth result in a lot of back buttons , ButtonHidden didn't help because just hide the button but the space is still there.
I want my application to have multiple functionality .
one view have a map , one an calendar and one note , for example . If I switch between View that happen , I got a lot of Back button . I want to know if is a solution for that navigationView to disappear and don't stack like that

SwiftUI restrict drag gesture that changes view in TabView

I have code that enables me to change views programatically using buttons by making use of
TabView(selection: $VM.selectedPage) {
ViewOne()
.tag(0)
...
}
And I am able to restrict users from progressing to the next view until user input is given, however they are still able to swipe through the views and bypass my restrictions.
I want to either add the same restrictions to the swipes as I have done with the buttons, or prevent the use of swiping to switch between views altogether.

How can I make a storyboard to the left of an initial view storyboard on Apple Watch using SwiftuI?

My Goal:
To make 3 views side by side, where each view takes up all of the Apple Watches viewable area. Something like Apple's workout app when it's on a current workout.
Using the stack overflow answer here I figured out how to use storyboards to make swipable views to the right of the Initial view, but how can I make a swipable view to the left of the initial storyboard view?
When I connect views with ctrl mouse from one view to another, the only segue option I get is next page, which makes a swipable view to the right.
Well, in SwiftUI 2.0 it looks like there is a very easy way to integrate this with a simple TabView and I'll show an example
struct threeViewsInOne{
#State private var selectedPage = 1
var body: some View {
TabView(selection: $selectedPage) {
ViewA.tag(0)
ViewB.tag(1)
ViewC.tag(2)
}
.tabViewStyle(PageTabViewStyle())
}
}

SwiftUI: disappearing popover on iPad only on some Views

I am experiencing problem with .popover() in SwiftUI it sometimes works correct, and other times it just appear and immediately disappear. Logging value of isPresented binding variable seems to show correct value.
Any idea why such thing can happen?
Moreover I have the same AddAttachment button that has popover() call added to different screen views and in one it works ok in other it stops working. Adding logs to init() of this screen views shows that value of isPresented #Binding var is ok (it is called several times (init()) but there is isPresented == true all the time after tapping button that should show popover). If popover disappear then taping button again changes #Binding var to false (so nothing show), and second consecutive tap make popover to appear and again immediately to disappear.

Why is the modifier .navigationBarTitle not applied to NavigationView?

In SwiftUI, I understand that modifiers are used to modify a view. When modifying a view, the modifier returns the view wrapped in _ModifiedContent.
When embedding my view inside a NavigationView and assigning a navigation bar title like this: (as seen in Apple's official tutorials)
NavigationView {
Text("Hello, World!")
.navigationBarTitle(Text("My first SwiftUI App"))
}
...why is the modifier for the navigation bar title applied to Text, not to NavigationView?
Like Martin linked to, navigationBarTitle only works when contained in NavigationView. This is discussed briefly in the SwiftUI Essentials talk at ~52:30. The title ends up flowing up to the NavigationView, so that views that get pushed can also provide a title of their own (e.g. the view that gets pushed at ~54:00).
If the title was attached to the NavigationView directly, all possible titles would need to be determined up front and removed from the views the titles are associated with.
It's similar to UIViewController's navigationItem property, which is attached to each pushed view instead of the UINavigationController itself.
Because the .navigationBarTitle() is modifier on the View inside of NavigationView, It is not on the NavigationView. And the same is the case for .navigationBarItem().
Please refer to Building Lists and Navigation