I am trying to implement a button that presents another scene with a "Slide from Botton" animation.
PresentationButton looked like a good candidate, so I gave it a try:
import SwiftUI
struct ContentView : View {
var body: some View {
NavigationView {
PresentationButton(destination: Green().frame(width: 1000.0)) {
Text("Click")
}.navigationBarTitle(Text("Navigation"))
}
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
Group {
ContentView()
.previewDevice("iPhone X")
.colorScheme(.dark)
ContentView()
.colorScheme(.dark)
.previewDevice("iPad Pro (12.9-inch) (3rd generation)"
)
}
}
}
#endif
I want the green view to cover the whole screen, and also the modal to be not "draggable to close".
Is it possible to add modifier to PresentationButton to make it full screen, and not draggable?
I have also tried a Navigation Button, but: - It doesn't "slide from bottom" - It creates a "back button" on detail view, which I don't want
thanks!
Although my other answer is currently correct, people probably want to be able to do this now. We can use the
Environment
to pass a view controller to children. Gist hereAdd an extension to UIViewController
And whenever we need it, use it:
[EDIT] Although it would be preferable to do something like
@Environment(\.viewController) var viewController: UIViewController?
this leads to a retain cycle. Therefore, you need to use the holder.This version fixes the compile error present in XCode 11.1 as well as ensures that controller is presented in the style that is passed in.
To use this version, the code is unchanged from the previous version.
Unfortunately, as of
Beta 2Beta 3, this is not possible in pure SwiftUI. You can see thatModal
has no parameters for anything likeUIModalPresentationStyle.fullScreen
. Likewise for PresentationButton.I suggest filing a radar.
The nearest you can currently do is something like:
Of course, from there you can add whatever transition you like in the overlay.
So I was struggling with that and I didn't like the overlay feature nor the ViewController wrapped version since it gave me some memory bug and I am very new to iOS and only know SwiftUI and no UIKit.
I developed credits the following with just SwiftUI which is probably what an overlay does but for my purposes it is much more flexible:
Adding an extension to
View
:Usage: Use a custom view and pass the
showModal
variable as aBinding<Bool>
to dismiss the modal from the view itself.I hope this helps!
Greetings krjw