如何从Viewcontroller中关闭swiftUI?

时间:2019-08-07 11:01:50

标签: swiftui uihostingcontroller

在我的viewController中,我具有此功能

...{
let vc = UIHostingController(rootView: SwiftUIView())
        present(vc, animated: true, completion: nil)
}

提供以下SwiftUIView。

Q如何在按下CustomButton时关闭SwiftUIView?

struct SwiftUIView : View {
     var body: some View {
       CustomButton()
     }   
}

struct CustomButton: View {

    var body: some View {
        Button(action: {
            self.buttonAction()
        }) {
            Text(buttonTitle)
        }
    }

    func buttonAction() {
        //dismiss the SwiftUIView when this button pressed
    }       
}

1 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp">
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:layout_gravity="center"
            android:ellipsize="end"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingBottom="8dp"
            android:textColor="@color/mineral"
            android:scrollHorizontally="true"
            android:maxLines="1"
            app:customTypeface="@string/font_sharp_sans_bold"
            tools:text="Hello Joel"/>
        <RelativeLayout
            android:id="@+id/select_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:background="@drawable/bg_selector">
            <TextView
                android:id="@+id/phone_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:layout_centerInParent="true"
                android:ellipsize="end"
                android:textColor="@color/charcoal"
                android:maxLines="1"
                app:customTypeface="@string/font_sharp_sans_semi_bold"
                tools:text="Samsung Galaxy"/>
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_drop_down"
                android:layout_centerVertical="true"
                android:rotation="180"
                android:layout_alignParentRight="true"/>
        </RelativeLayout>
    </LinearLayout>
</layout>

或者如果由于顶部有一个不同的视图控制器而无法正常工作,或者您需要使用视图生命周期事件(onDisappear和onAppear不适用于UIHostingController),则此方法无效。 您可以改用:

struct CustomButton: View {

    var body: some View {
        Button(action: {
            self.buttonAction()
        }) {
            Text(buttonTitle)
        }
    }

    func buttonAction() {
        if let topController = UIApplication.topViewController() {
            topController.dismiss(animated: true)
        }
    }       
}

extension UIApplication {
    class func topViewController(controller: UIViewController? = UIApplication.shared.windows.first { $0.isKeyWindow }?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            return topViewController(controller: navigationController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(controller: selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topViewController(controller: presented)
        }
        return controller
    }
}