How do I add swipe back gesture in swift? - ios

I found a question like this on Stack Overflow however it did not seem to work. Maybe I'm implementing it wrong, or it no longer works. I have a #Binding showDetailView that when equals true, shows the detail view.
This detail view has a custom back button that works. I want to be able to do a swipe gesture that performs the same function as the back button.
The code I found that looks like it could help is:
extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
The code for my main view (product page) is:
struct ProductPage: View {
#StateObject var MarketplaceModel = MarketplaceViewModel()
#State private var selectedMarketplaceFilter: MarketplaceFilterViewModel = .productList
#Namespace var animation : Namespace.ID
#State var showDetailProduct = false
#State var selectedProduct : Product!
var body: some View {
var columns = Array(repeating: GridItem(.flexible()), count: 2)
ZStack{
VStack(spacing: 10){
if MarketplaceModel.products.isEmpty{
Spacer()
ProgressView()
Spacer()
}
else{
ScrollView(.vertical, showsIndicators: false, content: {
LazyVGrid(columns: Array(repeating: GridItem(.flexible(),spacing: 10), count: 2),spacing: 20){
ForEach(MarketplaceModel.filteredProduct){product in
ProductView(productData: product)
.onTapGesture {
withAnimation {
selectedProduct = product
showDetailProduct.toggle()
}
}
}
}
})
}
}
if selectedProduct != nil && showDetailProduct{
ProductDetailView(showDetailProduct: $showDetailProduct, productData: selectedProduct, product_id)
.transition(.move(edge: .trailing))
}
}
}
}
The code for my detail view (product detail view) is:
struct ProductDetailView: View {
#StateObject var MarketplaceModel = MarketplaceViewModel()
#Binding var showDetailProduct: Bool
#Namespace var animation: Namespace.ID
#EnvironmentObject var marketplaceData: MarketplaceViewModel
var productData : Product
var product_id: String
var body: some View {
NavigationView{
VStack{
VStack{
// Title Bar...
HStack {
Button(action: {
withAnimation{showDetailProduct.toggle()}
}) {
Image(systemName: "arrow.backward.circle.fill")
Spacer()
ForEach(MarketplaceModel.product_details_array){ items in
Text(items.product_name)
.font(.largeTitle)
.fontWeight(.heavy)
.foregroundColor(.black)
}
}
}
ScrollView {
VStack {
Text(productData.product_name)
Text(productData.product_details)
}
}
}
}
}
}
}
How would I go about adding the back swipe gesture? (Running Xcode 13.4.1)

Related

How do I make my own focusable view in SwiftUI using FocusState?

So I want to implement a custom control as a UIViewRepresentable which correctly handles focus using an #FocusState binding.
So I want to be able to manage the focus like so:
struct MyControl: UIViewRepresentable { ... }
struct Container: View {
#FocusState var hasFocus: Bool = false
var body: some View {
VStack {
MyControl()
.focused($hasFocus)
Button("Focus my control") {
hasFocus = true
}
}
}
}
What do I have to implement in MyControl to have it respond to the focus state properly? Is there a protocol or something which must be implemented?
Disclaimer: the solution is not suitable for full custom controls. For these cases, you can try to pass the FocusState as binding: let isFieldInFocus: FocusState<Int?>.Binding
In my case, I have wrapped UITextView. In order to set the focus, I only used .focused($isFieldInFocus).
Some of the information can be obtained through the property wrapper(#Environment(\.)), but this trick does not work with focus.
struct ContentView: View {
#FocusState var isFieldInFocus: Bool
#State var text = "Test message"
#State var isDisabled = false
var body: some View {
VStack {
Text("Focus for UITextView")
.font(.headline)
AppTextView(text: $text)
.focused($isFieldInFocus)
.disabled(isDisabled)
.frame(height: 200)
HStack {
Button("Focus") {
isFieldInFocus = true
}
.buttonStyle(.borderedProminent)
Button("Enable/Disable") {
isDisabled.toggle()
}
.buttonStyle(.borderedProminent)
}
}
.padding()
.background(.gray)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct AppTextView: UIViewRepresentable {
#Binding var text: String
#Environment(\.isEnabled) var isEnabled: Bool
#Environment(\.isFocused) var isFocused: Bool // doesn't work
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.font = UIFont.preferredFont(forTextStyle: .body)
textView.delegate = context.coordinator
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text
print("isEnabled", isEnabled, "isFocused", isFocused)
}
class Coordinator : NSObject, UITextViewDelegate {
private var parent: AppTextView
init(_ textView: AppTextView) {
self.parent = textView
}
func textViewDidChange(_ textView: UITextView) {
parent.text = textView.text
}
}
}

SwiftUI, Dismiss modal/page from child view

I am new to Swift/SwiftUI and want to know how to dismiss modal/page from nested child view.
Firstly, I am calling from Flutter, UIHostingController, then SwiftUI page. (currently showing as modal...)
After Navigating to SwiftUI, I am not able to use #environment data from child view.
Is there any ways for this to work?
thanks in advance.
AppDelegate.swift
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller : FlutterViewController = self.window?.rootViewController as! FlutterViewController
let channel = FlutterMethodChannel.init(name: "com.example.show", binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler({
(call, result) -> Void in
if call.method == "sample" {
let vc = UIHostingController(rootView: ContentView())
vc.modalPresentationStyle = .fullScreen
controller.present(vc, animated: true,completion: nil)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ContentView.swift
struct ContentView: View{
#Environment(\.presentationMode) var presentation: Binding<PresentationMode>
private var childView: ChildView()
var body: some View{
NavigationView{
ZStack{
childView
Button(action: {
// This works ************************
self.presentation.wrappedValue.dismiss()
}, label: {
Text("close")
})
}
}
}
}
ChildView.swift
struct ChildView: View{
#Environment(\.presentationMode) var presentation: Binding<PresentationMode>
var body: some View{
Button(action: {
// This won't do anything *********************************
self.presentation.wrappedValue.dismiss()
// nor this↓ **********************************************
if #available(iOS 15.0, *) {
#Environment(\.dismiss) var dismiss;
dismiss()
dismiss.callAsFunction()
}
}, label: {
Text("close")
})
}
}
Since you had another NavigationView is your ContentView, the #Environment(\.presentation) inside ChildView is of a child and not the parent. Basically those two are from completely different Navigation stacks.
In order to still keep NavigationView inside your parent ContentView, you need to pass the presentation value from constructor of ChildView instead of environment:
ContentView.swift
struct ContentView: View{
#Environment(\.presentationMode) var presentation: Binding<PresentationMode>
var body: some View{
NavigationView{
ZStack{
ChildView(parentPresentation: presentation)
Button(action: {
self.presentation.wrappedValue.dismiss()
}, label: {
Text("close")
})
}
}
}
}
In child view, use normal property instead of #Environment
ChildView.swift
struct ChildView: View{
let parentPresentation: Binding<PresentationMode>
var body: some View{
Button(action: {
self.parentPresentation.wrappedValue.dismiss()
if #available(iOS 15.0, *) {
#Environment(\.dismiss) var dismiss;
dismiss()
dismiss.callAsFunction()
}
}, label: {
Text("Close")
})
}
}
For iOS 15.0 and above, we can use the new environment value dismiss, and for that to work with child view, we should also pass it from the parent view to the child view:
ContentView.swift
struct ContentView: View {
#Environment(\.dismiss) private var dismiss
var body: some View {
NavigationView {
ZStack {
ChildView(parentDismiss: dismiss)
Button {
dismiss()
} label: {
Text("close")
}
}
}
}
}
ChildView.swift
struct ChildView: View {
let parentDismiss: DismissAction
var body: some View {
Button {
parentDismiss()
} label: {
Text("Close")
}
}
}
I figured it out that NavigationView in ContentView.swift caused this issue.
Removing NavigationView, I could dismiss modal page from child view...
But this is not what I intended for :(...
var body: some View{
// NavigationView{ <--------
ZStack{
childView
Button(action: {
self.presentation.wrappedValue.dismiss()
}, label: {
Text("close")
})
}
// }
}

SwiftUI - #Published property is not updating view from coordinator

I'm using a wrapper around UISearchBar and I'm seeing a different behavior when passing a #Published property into this SearchBar wrapper versus a TextField.
Both are updating the #ObservedObject var query = Query() class #Published var input property as expected but only the TextField is then updating the SearchSheet view. I would like for the view to be updated when input has been changed in SearchBar similarly to how it is updated from TextField.
Edit: I've updated my question to include the ContentView where it looks like this issue is specific to when the sheet is called from a Button in a NavigationBarItem.
struct ContentView: View {
#State var showingSearch = false
var body: some View {
NavigationView {
VStack {
Text("Hello World")
}
.navigationBarItems(trailing:
Button(action: {
self.showingSearch.toggle()
}) {
Image(systemName: "magnifyingglass")
}
.sheet(isPresented: $showingSearch) {
SearchSheet(isPresented: self.$showingSearch)
}
)
}
}
}
class Query: ObservableObject {
#Published var input = "" {
didSet {
// Called as expected in both cases but only TextField updates the SearchSheet view.
}
}
}
struct SearchSheet: View {
#ObservedObject var query = Query()
var body: some View {
VStack {
// Does not update the SearchSheet view. I would like it understand why and how to update it.
SearchBar(text: $query.input, placeholder: "Search")
// Does update the SearchSheet view.
TextField("Search", text: $query.input)
Text("\(query.input)")
}
}
}
import SwiftUI
struct SearchBar: UIViewRepresentable {
#Binding var text: String
var placeholder: String
class Coordinator: NSObject, UISearchBarDelegate {
#Binding var text: String
init(text: Binding<String>) {
_text = text
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
text = searchText
}
}
func makeCoordinator() -> SearchBar.Coordinator {
return Coordinator(text: $text)
}
func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
let searchBar = UISearchBar(frame: .zero)
searchBar.delegate = context.coordinator
searchBar.placeholder = placeholder
searchBar.searchBarStyle = .minimal
searchBar.autocapitalizationType = .none
return searchBar
}
func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) {
uiView.text = text
}
}
struct SearchBar_Previews: PreviewProvider {
#State private static var text = ""
static var previews: some View {
SearchBar(text: $text, placeholder: "Search")
}
}
Moving the display of the sheet out from navigationBarItems resolves the issue. At the time I believe this is a bug.
.navigationBarItems(trailing:
Button(action: {
self.showingSearch.toggle()
}) {
Image(systemName: "magnifyingglass")
}
)
.sheet(isPresented: $showingSearch) {
SearchSheet(isPresented: self.$showingSearch)
}

In SwiftUI, the application freeze without any warning when slide back halfway but released before completion

The following code reproduced the error:
import SwiftUI
struct ContentView: View {
#State private var number: Int = 5
var body: some View {
NavigationView() {
VStack(spacing: 20) {
NavigationLink(destination: SecondView(bottles: $number)) {
Text("Click me")
}
}
}
}
}
struct SecondView: View {
#Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
#State private var color: UIColor = .black
#Binding var bottles: Int
var body: some View {
Text("I have \(bottles) in my bag")
.foregroundColor(Color(color))
.navigationBarTitle(Text("Water Bottle"))
.navigationBarItems(trailing:
Button("Click") {
self.someFunction()
}
)
}
func someFunction() {
if self.color == UIColor.black {
self.color = .red
} else {
self.color = .black
}
}
}
When sliding back from SecondView to ContentView but didn't complete the gesture, the app freezes. When deleting either #Environment or NavigationBarItem will fix this error.
For #Environment, it is needed for CoreData but used presentationMode for reproduction of error
adding ".navigationViewStyle(StackNavigationViewStyle())" to the NavigationView fix the problem for me. This is the code I use for testing this on real devices (iPhone, iPad) and various simulators. Using macos 10.15.5, Xcode 11.5 and 11.6 beta, target ios 13.5 and mac catalyst.
I have not tested this on all devices, so let me know if you find a device where this does not work.
import SwiftUI
struct ContentView: View {
#State private var number: Int = 5
var body: some View {
NavigationView() {
VStack(spacing: 20) {
NavigationLink(destination: SecondView(bottles: $number)) {
Text("Click me")
}
}
}.navigationViewStyle(StackNavigationViewStyle()) // <---
}
}
struct SecondView: View {
#Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
#State private var color: UIColor = .black
#Binding var bottles: Int
var body: some View {
Text("I have \(bottles) in my bag")
.foregroundColor(Color(color))
.navigationBarTitle(Text("Water Bottle"))
.navigationBarItems(trailing:
Button("Click") {
self.someFunction()
}
)
}
func someFunction() {
if self.color == UIColor.black {
self.color = .red
} else {
self.color = .black
}
}
}

Scroll info from SwiftUI List - UIViewRepresentable UIScrollView List bindings not always working with SwiftUI

I'm trying to observe/get some of SwiftUI's List scrolling attributes by wrapping/injection of UIScrollView using UIViewRepresentable.
I'm getting inconsistent behavior with bindings.
Pressing the buttons and changing values depending on if the button is in the parent vs child view has different results.
The bindings from my ObservableObject ScrollInfo class and the UIViewRepresentable start fine, but then break, unless the whole screen is refreshed and makeUIView runs again (like changing to a different tab).
Is there a way to force the UIViewRepresentable to run makeUIView again on a binding update? Or something that will fix this?
I'd like for isScrolling values to be updated and working all the time.
I set up a test to change the colors of the circles to red if the user is dragging the scrollview down. It works initially but stops if I update a value from the ObservableObject in the parent view.
Screenshots of Test from code below
Bindings keep working with bottom button press (updating ObservableObject) in child view
Bindings break with top button press (updating ObservableObject) in parent view
// Parent View
import SwiftUI
struct ContentView: View {
#ObservedObject var scrollInfo:ScrollInfo = ScrollInfo()
var body: some View {
VStack
{
Button(action:{
self.scrollInfo.contentLoaded = true;
})
{
Text("REFRESH")
}
TestView()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
// Child View
import SwiftUI
struct TestView: View {
#State var test1:String = "Test1"
#ObservedObject var scrollInfo:ScrollInfo = ScrollInfo()
var body: some View {
VStack
{
Button(action:{
self.scrollInfo.contentLoaded.toggle()
}) {
Text("REFRESH")
}
List{
VStack {
VStack{
Text(String( self.scrollInfo.contentLoaded))
Text(self.test1)
Circle().frame(width:50,height:50).foregroundColor(self.scrollInfo.isScrolling ? .red : .green)
}
VStack{
Text(self.scrollInfo.text)
Text(self.test1)
Circle().frame(width:50,height:50).foregroundColor(self.scrollInfo.isScrolling ? .red : .green)
}
VStack{
Text(self.scrollInfo.text)
Text(self.test1)
Circle().frame(width:50,height:50).foregroundColor(self.scrollInfo.isScrolling ? .red : .green)
}
VStack{
Text(self.scrollInfo.text)
Text(self.test1)
Circle().frame(width:50,height:50).foregroundColor(self.scrollInfo.isScrolling ? .red : .green)
}
} .padding(.bottom,620).padding(.top,20)
.background(
ListScrollingHelper(scrollInfo: self.scrollInfo)// injection
)
}.padding(.top,4).onAppear(perform:{
})
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
// ScrollInfo Class
class ScrollInfo: ObservableObject {
#Published var isScrolling:Bool = false
#Published var text:String = "Blank"
#Published var contentLoaded:Bool = false
init()
{
print( "scrollInfo init")
}
deinit {
print("scrollInfo denit")
}
}
// UIViewRepresentable
final class ListScrollingHelper: UIViewRepresentable {
var scrollInfo:ScrollInfo
#Published var scrollView: UIScrollView?
init( scrollInfo:ScrollInfo) {
print("init UIViewRepresentable listscrolling helper")
self.scrollInfo = scrollInfo
}
func makeUIView(context: Context) -> UIView {
//self.uiScrollView.delegate = context.coordinator
print("makeview")
return UIView()
//return self.uiScrollView // managed by SwiftUI, no overloads
}
func catchScrollView(for view: UIView) {
print("checking for scrollview")
if nil == scrollView {
scrollView = view.enclosingScrollView()
if(scrollView != nil)
{
print("scrollview found")
}
}
}
func updateUIView(_ uiView: UIView, context: Context) {
catchScrollView(for: uiView)
if(scrollView != nil)
{
scrollView!.delegate = context.coordinator
}
print("updatingUIView")
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject,UIScrollViewDelegate {
var parent: ListScrollingHelper
init(_ listScrollingHelper: ListScrollingHelper) {
self.parent = listScrollingHelper
print("init coordinator")
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
// DispatchQueue.main.async {
if(!self.parent.scrollInfo.isScrolling)
{
self.parent.scrollInfo.isScrolling = true
//self.parent.scrollInfo.text = "scroll"
// }
}
print("start scroll")
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if(self.parent.scrollInfo.isScrolling && !decelerate)
{
self.parent.scrollInfo.isScrolling = false
}
print("end scroll")
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if(self.parent.scrollInfo.isScrolling)
{
self.parent.scrollInfo.isScrolling = false
}
print("end scroll")
}
deinit
{
print("deinit coordinator")
}
}
deinit {
print("deinit UIViewRepresentable listscrolling helper")
}
}
extension UIView {
func enclosingScrollView() -> UIScrollView? {
var next: UIView? = self
repeat {
next = next?.superview
if let scrollview = next as? UIScrollView {
return scrollview
}
} while next != nil
return nil
}
}
EDIT - WORKAROUND
I wasn't able to get it working with the ObservableObject or EnvironmentObject, but I was able to get it working with #State and #Binding, although it's a limited amount of info passed back. (ScrollInfo is still there only to use to testing changing a parent ObservableObject)
Hope this helps someone else!
import SwiftUI
struct TestView: View {
#State var isScrolling:Bool = false;
var body: some View {
VStack
{
Button(action:{
self.scrollInfo.contentLoaded.toggle()
}) {
Text("REFRESH")
}
List{
VStack {
VStack{
Text("isScrolling")
Text(String(self.isScrolling))
Circle().frame(width:50,height:50).foregroundColor(self.isScrolling ? .red : .green)
}
VStack{
Text(self.scrollInfo.text)
Text(self.test1)
Circle().frame(width:50,height:50).foregroundColor(self.isScrolling ? .red : .green)
}
VStack{
Text(self.scrollInfo.text)
Text(self.test1)
Circle().frame(width:50,height:50).foregroundColor(self.isScrolling ? .red : .green)
}
VStack{
Text(self.scrollInfo.text)
Text(self.test1)
Circle().frame(width:50,height:50).foregroundColor(self.isScrolling ? .red : .green)
}
} .padding(.bottom,620).padding(.top,20).background( ListScrollingHelper(isScrolling: self.$isScrolling))
}.padding(.top,4)
}
}
}
extension UIView {
func enclosingScrollView() -> UIScrollView? {
var next: UIView? = self
repeat {
next = next?.superview
if let scrollview = next as? UIScrollView {
return scrollview
}
} while next != nil
return nil
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
final class ListScrollingHelper: UIViewRepresentable {
#Binding var isScrolling:Bool
private var scrollView: UIScrollView?
init(isScrolling:Binding<Bool>)
{
self._isScrolling = isScrolling
}
func makeUIView(context: Context) -> UIView {
return UIView()
}
func catchScrollView(for view: UIView) {
if nil == scrollView {
scrollView = view.enclosingScrollView()
}
}
func updateUIView(_ uiView: UIView, context: Context) {
catchScrollView(for: uiView)
if(scrollView != nil)
{
scrollView!.delegate = context.coordinator
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject,UIScrollViewDelegate {
var parent: ListScrollingHelper
init(_ listScrollingHelper: ListScrollingHelper) {
self.parent = listScrollingHelper
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if(!self.parent.isScrolling)
{
self.parent.isScrolling = true
}
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if(self.parent.isScrolling && !decelerate)
{
self.parent.isScrolling = false
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if(self.parent.isScrolling)
{
self.parent.isScrolling = false
}
}
deinit
{
}
}
deinit {
}
}
class ScrollInfo: ObservableObject {
#Published var isScrolling:Bool = false
#Published var text:String = "Blank"
#Published var contentLoaded:Bool = false
}

Resources